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 2 3 4 5 |
[root@localhost /]# mkdir -p /data/discuz/www [root@localhost /]# mkdir /data/logs/discuz/ -p [root@localhost nginx]# vim vhosts.conf |
注:创建需要的目录。编辑虚拟主机配置文件,并加入以下内容
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 2 3 |
[root@localhost discuz]# mv www/ www1/ [root@localhost discuz]# /usr/local/nginx/sbin/nginx -s reload |
注:将Nginx上面的www发布目录重命名为www1,此时访问Nginx不会加载静态页面,如下图
1 2 3 |
[root@localhost discuz]# mv www1/ www/ [root@localhost discuz]# /usr/local/nginx/sbin/nginx -s reload |
注:将发布目录还原回去,然后重启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即可访问之前的页面
配置方法如下图:
未修改之前访问页面:
修改之后访问页面: