Nginx动静分离经典案例配置

随着Nginx高性能Web服务器大量被使用,目前Nginx最新稳定版为1.2.6,张宴兄在实际应用中大量使用Nginx,并分享Nginx高性能Web服务器知识,使得Nginx在国内也是飞速的发展。那今天咱们再来温习一下Nginx 动静分离知识,这里仅供参考。

一、实践环境:

  1. 系统版本:CentOS6.0 X86_64 
  2.  
  3. Nginx版本:Nginx-1.2.6 
  4.  
  5. Tomcat版本:Tomcat-6.0.18 

二、Nginx安装:

   实际环境中安装Nginx,首先需要安装pcre库,然后再安装Nginx:

  1. #安装pcre支持rewrite库,也可以安装源码,注*安装源码时,指定pcre路径为解压源码的路径,而不是编译后的路径,否则会报错。
  2. yum install pcre-devel pcre -y
  3.  
  4. #下载Nginx源码包
  5. cd /usr/src ;wget -c http://nginx.org/download/nginx-1.2.6.tar.gz
  6.  
  7. #解压Nginx源码包
  8. tar -xzf nginx-1.2.6.tar.gz 
  9.  
  10. #进入解压目录,然后sed修改Nginx版本信息为TDTWS 
  11. cd nginx-1.2.6 ; sed -i -e 's/1.2.6//g' -e 's/nginx\//TDTWS/g' -e 's/"NGINX"/"TDTWS"/g' src/core/nginx.h 
  12.  
  13. #预编译Nginx
  14. ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  15. #.configure预编译成功后,执行make命令进行编译
  16.  
  17. make
  18. #make执行成功后,执行make install 正式安装
  19. make install
  20. #自此Nginx安装完毕!!! 

三、配置Nginx:

   这里鉴于我的51CTO博客已经有Tomcat安装和配置了,这里忽略,只配置Nginx。

  1. #进入Nginx应用目录 
  2. cd /usr/local/nginx/conf 
  3. #备份原nginx.conf文件 
  4. mv  nginx.conf  nginx.bak  

   创建 vi nginx.conf ,并写入如下内容:

  1. user www www; 
  2. worker_processes 8; 
  3. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; 
  4. pid /usr/local/nginx/nginx.pid; 
  5.  
  6. worker_rlimit_nofile 102400; 
  7.  
  8. events 
  9. use epoll; 
  10. worker_connections 102400; 
  11. http 
  12.   include       mime.types; 
  13.    
  14.   default_type  application/octet-stream; 
  15.  
  16.   fastcgi_intercept_errors on; 
  17.  
  18.   charset  utf-8; 
  19.  
  20.   server_names_hash_bucket_size 128; 
  21.   client_header_buffer_size 4k; 
  22.   large_client_header_buffers 4 32k; 
  23.   client_max_body_size 300m; 
  24.  
  25.   sendfile on; 
  26.   tcp_nopush     on; 
  27.  
  28.   keepalive_timeout 60; 
  29.  
  30.   tcp_nodelay on; 
  31.  
  32.   client_body_buffer_size  512k; 
  33.   proxy_connect_timeout    5; 
  34.   proxy_read_timeout       60; 
  35.   proxy_send_timeout       5; 
  36.   proxy_buffer_size        16k; 
  37.   proxy_buffers            4 64k; 
  38.   proxy_busy_buffers_size 128k; 
  39.   proxy_temp_file_write_size 128k; 
  40.  
  41.   gzip on; 
  42.   gzip_min_length  1k; 
  43.   gzip_buffers     4 16k; 
  44.   gzip_http_version 1.1; 
  45.   gzip_comp_level 2; 
  46.   gzip_types       text/plain application/x-javascript text/css application/xml; 
  47.   gzip_vary on; 
  48.  
  49.    
  50. ###2012-12-19 change nginx logs 
  51.  
  52. log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" ' 
  53.               '$status $body_bytes_sent "$http_referer" ' 
  54.               '"$http_user_agent"  $request_time $remote_addr'; 
  55.  
  56. #这里为后端服务器wugk应用集群配置,根据后端实际情况修改即可,tdt_wugk为负载均衡名称,可以任意指定
  57. #但必须跟vhosts.conf虚拟主机的pass段一致,否则不能转发后端的请求。 
  58. upstream tdt_wugk { 
  59.     server   10.10.141.30:8080 weight=1 max_fails=2 fail_timeout=30s
  60.     server   10.10.141.30:8081 weight=1 max_fails=2 fail_timeout=30s
  61.     server   10.10.141.31:8080 weight=1 max_fails=2 fail_timeout=30s
  62.     server   10.10.141.31:8081 weight=1 max_fails=2 fail_timeout=30s
  63.     server   10.10.141.32:8080 weight=1 max_fails=2 fail_timeout=30s
  64.     server   10.10.141.32:8081 weight=1 max_fails=2 fail_timeout=30s
  65. #这里为后端APP应用负载均衡配置,根据后端实际情况修改即可。tdt_app为负载均衡名称,可以任意指定
  66. upstream tdt_app { 
  67.     server   10.10.141.40:8080 weight=1 max_fails=2 fail_timeout=30s
  68.     server   10.10.141.40:8081 weight=1 max_fails=2 fail_timeout=30s
  69.     server   10.10.141.41:8080 weight=1 max_fails=2 fail_timeout=30s
  70.     server   10.10.141.41:8081 weight=1 max_fails=2 fail_timeout=30s
  71.     server   10.10.141.42:8080 weight=1 max_fails=2 fail_timeout=30s
  72.     server   10.10.141.42:8081 weight=1 max_fails=2 fail_timeout=30s
  73. #include引用vhosts.conf,该文件主要用于配置Nginx 虚拟主机  
  74. include vhosts.conf; 

   如上nginx.conf配置完毕,继续配置nginx虚拟主机,继续在当前目录创建vhosts.conf

   vi vhosts.conf 内容如下:

  1. ####www.wuguangke.cn 
  2. server 
  3.  
  4.   { 
  5.     listen       80; 
  6.     server_name  www.wuguangke.cn; 
  7.     index index.html index.htm; 
  8. #配置发布目录为/data/www/wugk  
  9.     root  /data/www/wugk; 
  10.  
  11.     location / 
  12.     { 
  13.          proxy_next_upstream http_502 http_504 error timeout invalid_header; 
  14.          proxy_set_header Host  $host; 
  15.          proxy_set_header X-Real-IP $remote_addr; 
  16.       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  17.          proxy_pass http://tdt_wugk; 
  18.          expires      3d; 
  19.     } 
  20.     #动态页面交给http://tdt_wugk,也即我们之前在nginx.conf定义的upstream tdt_wugk 均衡 
  21.     location ~ .*\.(php|jsp|cgi)?$ 
  22.     { 
  23.          proxy_set_header Host  $host; 
  24.          proxy_set_header X-Real-IP $remote_addr; 
  25.       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  26.          proxy_pass http://tdt_wugk; 
  27.     } 
  28.     #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
  29.     location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
  30.     { 
  31.     root /data/www/wugk; 
  32. #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
  33.     expires      3d; 
  34.     } 
  35.     #定义Nginx输出日志的路径 
  36.     access_log  /data/logs/nginx_wugk/access.log main; 
  37.     error_log   /data/logs/nginx_wugk/error.log  crit; 
  38.  
  39. ##########chinaapp.sinaapp.com 2012-12-19 
  40.   server 
  41.  
  42.   { 
  43.     listen       80; 
  44.     server_name  chinaapp.sinaapp.com; 
  45.     index index.html index.htm; 
  46.     root  /data/www; 
  47.  
  48.     location / 
  49.     { 
  50.          proxy_next_upstream http_502 http_504 error timeout invalid_header; 
  51.          proxy_set_header Host  $host; 
  52.          proxy_set_header X-Real-IP $remote_addr; 
  53.      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  54.          proxy_pass http://tdt_app; 
  55.          expires      3d; 
  56.     } 
  57.  
  58.     location ~ .*\.(php|jsp|cgi)?$ 
  59.     { 
  60.          proxy_set_header Host  $host; 
  61.          proxy_set_header X-Real-IP $remote_addr; 
  62.       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  63.          proxy_pass http://tdt_app; 
  64.     } 
  65.     location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
  66.     { 
  67.     root /data/www/app; 
  68.     expires      3d; 
  69.     } 
  70.      
  71.     access_log  /data/logs/nginx_app/access.log main; 
  72.     error_log   /data/logs/nginx_app/error.log  crit; 

四、部署测试:

   后端配置好Tomcat服务,并启动,发布的程序需同步到Nginx的/data/www对应的目录,因为配置动静分离后,用户请求你定义的静态页面,默认会去nginx的发布目录请求,而不会到后端请求,所以这时候你要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步。

  1. #检查Nginx配置文件是否配置正确,提示Ok and successful表示正确,如下:  
  2. [root@WEB-11-151 ~]# /usr/local/nginx/sbin/nginx -t
  3. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  4. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  5. #启动Nginx服务 
  6. /usr/local/nginx/sbin/nginx 
  7. #查看Nginx进程是否启动 
  8. ps -ef |grep nginx 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值