Nginx反向代理、動靜分離、負載均衡及rewrite隱藏路徑詳解
(Nginx Apache MySQL Redis)
楓城浪子原創,轉載請標明出處!
更多技術博文請見個人博客:https://fengchenglangzi.000webhostapp.com
微信bh19890922
QQ445718526、490425557
三、Nginx動靜分離及負載均衡
3.1 Nginx安裝
請參考:https://fengchenglangzi.000webhostapp.com/?p=511
亦可參考:http://fengchenglangzi.blog.51cto.com/1322043/1961309
3.2 動靜分離及負載均衡
3.2.1 動靜分離原理
Nginx動靜分離簡單來說就是把動態跟靜態請求分開,不能理解成只是單純的把動態頁面和靜態頁面物理分離。嚴格意義上說應該是動態請求跟靜態請求分開,可以理解成使用Nginx處理靜態頁面,Tomcat、Resin、PHP、ASP處理動態頁面。
動靜分離從目前實現角度來講大致分爲兩種,一種是純粹的把靜態文件獨立成單獨的域名,放在獨立的服務器上,也是目前主流推崇的方案;另外一種方法就是動態跟靜態文件混合在一起發佈,通過Nginx來分開。
3.2.2 動靜分離配置
1 | [root@localhost conf]# vim nginx.conf |
注:編輯Nginx主配置文件,並修改爲以下內容
pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 102400; events { use epoll; worker_connections 102400; } http { include mime.types; default_type application/octet-stream; FastCGI_intercept_errors on; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 4k; server_names_hash_bucket_size 128; client_header_buffer_size 4k; user nginx nginx; worker_processes 8; pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 102400; events { use epoll; worker_connections 102400; } http { include mime.types; default_type application/octet-stream; FastCGI_intercept_errors on; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 4k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ ‘”$http_user_agent” $request_time’; upstream discuz { server 192.168.8.135:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.8.136:80 weight=1 max_fails=2 fail_timeout=30s; } include vhosts.conf; } |
注:upstream discus 表示配置負載均衡,include vhosts.conf代表引用虛擬主機
1 | [root@localhost nginx]# touch vhosts.conf |
注:創建虛擬主機文件
1 | [root@localhost /]# mkdir -p /data/discuz/www |
注:創建需要的目錄。編輯虛擬主機配置文件,並加入以下內容
server { listen 80; server_name localhost; index index.jsp index.html index.htm; root /data/discuz/www; location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://discuz; } location ~ .*\.(php|jsp|cgi|shtml)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://discuz; } location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /data/discuz/www; expires 30d; } access_log /data/logs/discuz/access.log main; error_log /data/logs/discuz/error.log crit; } |
配置文件代碼中:location ~ .*\.(php|jsp|cgi|shtml)表示匹配動態頁面請求,然後將請求proxy_pass到後端服務器,而location ~ .*\.(html|htm|gif|jpg|jpeg |ico|txt|js|css)表示匹配靜態頁面請求本地返回。
3.2.3 測試動靜分離
1 | [root@localhost discuz]# mv www/ www1/ |
注:將Nginx上面的www發佈目錄重命名爲www1,此時訪問Nginx不會加載靜態頁面,如下圖
1 | [root@localhost discuz]# mv www1/ www/ |
注:將發佈目錄還原回去,然後重啓Nginx,如下圖
到此,Nginx動靜分離成功
3.2.4 測試負載均衡
1 | [root@localhost htdocs]# tail -fn 10 /usr/local/apache2/logs/access_log |
注:在兩臺LAP服務器上面查看訪問日誌,刷新頁面,並查看兩臺LAP訪問日誌會進行輪詢響應用戶請求,如下圖
至此Nginx負載均衡配置完成!
四、Nginx Rewrite隱藏真實路徑
之前訪問discuz論壇地址爲:http://192.168.8.134/forum.php
配置rewrite把後面forum.php隱藏直接輸入http://192.168.8.134即可訪問之前的頁面
配置方法如下圖:
未修改之前訪問頁面:
修改之後訪問頁面: