标签:nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解(nginx apache mysql redis)–第三部分
Nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解
(Nginx Apache MySQL Redis)
楓城浪子原创,转载请标明出处!
更多技术博文请见个人博客:https://fengchenglangzi.000webhostapp.com
微信bh19890922
QQ445718526、490425557
请参考:https://fengchenglangzi.000webhostapp.com/?p=511
亦可参考:http://fengchenglangzi.blog.51cto.com/1322043/1961309
Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat、Resin、PHP、ASP处理动态页面。
动静分离从目前实现角度来讲大致分为两种,一种是纯粹的把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过Nginx来分开。
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)表示匹配静态页面请求本地返回。
1 | [root@localhost discuz]# mv www/ www1/ |
注:将Nginx上面的www发布目录重命名为www1,此时访问Nginx不会加载静态页面,如下图
1 | [root@localhost discuz]# mv www1/ www/ |
注:将发布目录还原回去,然后重启Nginx,如下图
到此,Nginx动静分离成功
1 | [root@localhost htdocs]# tail -fn 10 /usr/local/apache2/logs/access_log |
注:在两台LAP服务器上面查看访问日志,刷新页面,并查看两台LAP访问日志会进行轮询响应用户请求,如下图
至此Nginx负载均衡配置完成!
之前访问discuz论坛地址为:http://192.168.8.134/forum.php
配置rewrite把后面forum.php隐藏直接输入http://192.168.8.134即可访问之前的页面
配置方法如下图:
未修改之前访问页面:
修改之后访问页面:
本文出自 “楓城浪子” 博客,请务必保留此出处http://fengchenglangzi.blog.51cto.com/1322043/1963224
Nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第三部分
标签:nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解(nginx apache mysql redis)–第三部分
原文地址:http://fengchenglangzi.blog.51cto.com/1322043/1963224