linux安裝nginx搭建服務並實現文件服務器和代理服務器配置
1.課題的背景和意義
由於編碼過程中需要進行文件上傳服務,文件上傳后 需要有http資源的路徑需要訪問。原則上可以通過Apache 、iis 、nginx 等方式映射文件夾為網站即可實現。由於使用linux環境,nginx安裝方便快捷,故采用nginx來實現;
域名資源寶貴,申請額外的域名流程繁瑣;
需要多個文件服務器,使用同一個域名代理進行訪問。
2.環境資料准備
linux服務器一台 centos
nginx 依賴包 gcc zlib zlib-devel pcre-devel openssl openssl-devel 等;
nginx 安裝包 http://nginx.org/en/download.html
3.安裝、設計與實現
設計思路:
第一步 安裝依賴包:yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
第二步 解壓、編譯nginx源碼並安裝
tar -xvf nginx-1.19.8.tar.gz
cd nginx-1.19.8/
./configure (若依賴包未安裝 此處會報錯,安裝響應的依賴包即可 yum -y install gcc-c++)
make
make install 默認安裝目錄為:/usr/local/nginx/ 配置文件位置:/usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf 修改配置文件
cd /usr/local/nginx
./nginx 運行 若修改配置文件后 需要reload nginx 命令為:./nginx -s reload
第三步:使用nginx 搭建三台服務器、 並搭建代理服務器,使用二級目錄代理前面提到的三台服務器;
1.建立文件目錄三個;
/mydata/weixin_sc/images
/mydata/weixin_sc/uploadfiles
/mydata/weixin_sc/wxweb
2 nginx.conf修改配置 :
#user nobody;
worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #文件服務器1 server { listen 8991; server_name localhost; location / { root /mydata/weixin_sc/images; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #文件服務器2 server { listen 8992; server_name localhost; location / { root /mydata/weixin_sc/uploadfiles; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #文件服務器3 server { listen 8993; server_name localhost; location / { root /mydata/weixin_sc/wxweb; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #代理服務器 server { listen 8994; #listen 443 ssl; server_name localhost; #client_max_body_size 10M; #啟用 SSL 功能, deprecated #ssl on; #證書文件名稱 #ssl_certificate #私鑰文件名稱 #ssl_certificate_key #ssl_session_timeout 5m; #請按照以下協議配置 #ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #請按照以下套件配置,配置加密套件,寫法遵循 openssl 標准。 #ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #ssl_prefer_server_ciphers on; add_header X-Frame-Options SAMEORIGIN; #不允許iframe嵌套 location / { proxy_pass http://localhost:8991; } #使用二級目錄代理三台文件服務器,節約域名。; 注意 代理路徑 /,有無/會影響代理服務,是否傳遞目錄問題; location /images/ { proxy_pass http://localhost:8991/; } location /uploadfiles/ { proxy_pass http://localhost:8992/; } location /wxweb/ { proxy_pass http://localhost:8993/; } } }
重啟ng服務: ./nginx -s reload
擴展學習:
查看nginx進程是否啟動:
ps -ef | grep nginx
啟動,關閉,重啟,命令:
./nginx 啟動
./nginx -s stop 關閉
./nginx -s reload 重啟
參考文獻:
http://nginx.org/
http://nginx.org/en/docs/