---
title: "Nginx动静分离经典案例配置"
author: "myangle89"
date: "2013-02-20T11:20:54Z"
tags: []
categories: ["Web前端"]
slug: "nginx"
draft: false
lastmod: "2013-02-20T11:20:54Z"
id: 1808293
url: "https://myangle89.iteye.com/blog/1808293"
---

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

一、实践环境：

1. 系统版本：CentOS6.0 X86\_64
2. Nginx版本：Nginx-1.2.6
3. Tomcat版本：Tomcat-6.0.18

二、Nginx安装：

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

01. #安装pcre支持rewrite库,也可以安装源码，注\*安装源码时，指定pcre路径为解压源码的路径，而不是编译后的路径，否则会报错。
02. yum install pcre-devel pcre -y
03. #下载Nginx源码包
04. cd /usr/src ;wget -c http://nginx.org/download/nginx-1.2.6.tar.gz
05. #解压Nginx源码包
06. tar -xzf nginx-1.2.6.tar.gz
07. #进入解压目录，然后sed修改Nginx版本信息为TDTWS
08. 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
09. #预编译Nginx
10. ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http\_stub\_status\_module --with-http\_ssl\_module
11. #.configure预编译成功后，执行make命令进行编译
12. make
13. #make执行成功后，执行make install 正式安装
14. make install
15. #自此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 ，并写入如下内容:

01. user www www;
02. worker\_processes 8;
03. worker\_cpu\_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
04. pid /usr/local/nginx/nginx.pid;
05. worker\_rlimit\_nofile 102400;
06. events
07. {
08. use epoll;
09. worker\_connections 102400;
10. }
11. http
12. {
13. include       mime.types;
14. default\_type  application/octet-stream;
15. fastcgi\_intercept\_errors on;
16. charset  utf-8;
17. server\_names\_hash\_bucket\_size 128;
18. client\_header\_buffer\_size 4k;
19. large\_client\_header\_buffers 4 32k;
20. client\_max\_body\_size 300m;
21. sendfile on;
22. tcp\_nopush     on;
23. keepalive\_timeout 60;
24. tcp\_nodelay on;
25. client\_body\_buffer\_size  512k;
26. proxy\_connect\_timeout    5;
27. proxy\_read\_timeout       60;
28. proxy\_send\_timeout       5;
29. proxy\_buffer\_size        16k;
30. proxy\_buffers            4 64k;
31. proxy\_busy\_buffers\_size 128k;
32. proxy\_temp\_file\_write\_size 128k;
33. gzip on;
34. gzip\_min\_length  1k;
35. gzip\_buffers     4 16k;
36. gzip\_http\_version 1.1;
37. gzip\_comp\_level 2;
38. gzip\_types       text/plain application/x-javascript text/css application/xml;
39. gzip\_vary on;
40. ###2012-12-19 change nginx logs
41. log\_format  main  '$http\_x\_forwarded\_for - $remote\_user \[$time\_local] "$request" '
42. '$status $body\_bytes\_sent "$http\_referer" '
43. '"$http\_user\_agent"  $request\_time $remote\_addr';
44. #这里为后端服务器wugk应用集群配置，根据后端实际情况修改即可，tdt\_wugk为负载均衡名称，可以任意指定
45. #但必须跟vhosts.conf虚拟主机的pass段一致，否则不能转发后端的请求。
46. upstream tdt\_wugk {
47. server   10.10.141.30:8080 weight=1 max\_fails=2 fail\_timeout=30s;
48. server   10.10.141.30:8081 weight=1 max\_fails=2 fail\_timeout=30s;
49. server   10.10.141.31:8080 weight=1 max\_fails=2 fail\_timeout=30s;
50. server   10.10.141.31:8081 weight=1 max\_fails=2 fail\_timeout=30s;
51. server   10.10.141.32:8080 weight=1 max\_fails=2 fail\_timeout=30s;
52. server   10.10.141.32:8081 weight=1 max\_fails=2 fail\_timeout=30s;
53. }
54. #这里为后端APP应用负载均衡配置，根据后端实际情况修改即可。tdt\_app为负载均衡名称，可以任意指定
55. upstream tdt\_app {
56. server   10.10.141.40:8080 weight=1 max\_fails=2 fail\_timeout=30s;
57. server   10.10.141.40:8081 weight=1 max\_fails=2 fail\_timeout=30s;
58. server   10.10.141.41:8080 weight=1 max\_fails=2 fail\_timeout=30s;
59. server   10.10.141.41:8081 weight=1 max\_fails=2 fail\_timeout=30s;
60. server   10.10.141.42:8080 weight=1 max\_fails=2 fail\_timeout=30s;
61. server   10.10.141.42:8081 weight=1 max\_fails=2 fail\_timeout=30s;
62. }
63. #include引用vhosts.conf，该文件主要用于配置Nginx 虚拟主机
64. include vhosts.conf;
65. }

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

   vi vhosts.conf 内容如下：

01. ####www.wuguangke.cn
02. server
03. {
04. listen       80;
05. server\_name  www.wuguangke.cn;
06. index index.html index.htm;
07. #配置发布目录为/data/www/wugk
08. root  /data/www/wugk;
09. location /
10. {
11. proxy\_next\_upstream http\_502 http\_504 error timeout invalid\_header;
12. proxy\_set\_header Host  $host;
13. proxy\_set\_header X-Real-IP $remote\_addr;
14. proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
15. proxy\_pass http://tdt\_wugk;
16. expires      3d;
17. }
18. #动态页面交给http://tdt\_wugk，也即我们之前在nginx.conf定义的upstream tdt\_wugk 均衡
19. location ~ .\*\\.(php|jsp|cgi)?$
20. {
21. proxy\_set\_header Host  $host;
22. proxy\_set\_header X-Real-IP $remote\_addr;
23. proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
24. proxy\_pass http://tdt\_wugk;
25. }
26. #配置Nginx动静分离，定义的静态页面直接从Nginx发布目录读取。
27. location ~ .\*\\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
28. {
29. root /data/www/wugk;
30. #expires定义用户浏览器缓存的时间为3天，如果静态页面不常更新，可以设置更长，这样可以节省带宽和缓解服务器的压力
31. expires      3d;
32. }
33. #定义Nginx输出日志的路径
34. access\_log  /data/logs/nginx\_wugk/access.log main;
35. error\_log   /data/logs/nginx\_wugk/error.log  crit;
36. }
37. ##########chinaapp.sinaapp.com 2012-12-19
38. server
39. {
40. listen       80;
41. server\_name  chinaapp.sinaapp.com;
42. index index.html index.htm;
43. root  /data/www;
44. location /
45. {
46. proxy\_next\_upstream http\_502 http\_504 error timeout invalid\_header;
47. proxy\_set\_header Host  $host;
48. proxy\_set\_header X-Real-IP $remote\_addr;
49. proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
50. proxy\_pass http://tdt\_app;
51. expires      3d;
52. }
53. location ~ .\*\\.(php|jsp|cgi)?$
54. {
55. proxy\_set\_header Host  $host;
56. proxy\_set\_header X-Real-IP $remote\_addr;
57. proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
58. proxy\_pass http://tdt\_app;
59. }
60. location ~ .\*\\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
61. {
62. root /data/www/app;
63. expires      3d;
64. }
65. access\_log  /data/logs/nginx\_app/access.log main;
66. error\_log   /data/logs/nginx\_app/error.log  crit;
67. }

四、部署测试：

   后端配置好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

  本文只是一个简单的实际案例，里面的配置和参数这里没有做过多的说明，后期会继续更新。

本文出自 “[烟雨楼台](http://wgkgood.blog.51cto.com/)” 博客，请务必保留此出处[http://wgkgood.blog.51cto.com/1192594/1094236](http://wgkgood.blog.51cto.com/1192594/1094236)