Nginx動靜分離經典案例配置

隨着Nginx高性能Web服務器大量被使用,目前Nginx最新穩定版爲1.2.6,張宴兄在實際應用中大量使用Nginx,並分享Nginx高性能Web服務器知識,使得Nginx在國內也是飛速的發展。那今天咱們再來溫習一下Nginx 動靜分離知識,這裏僅供參考。

一、實踐環境:

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

二、Nginx安裝:

  實際環境中安裝Nginx,首先需要安裝pcre庫,然後再安裝Nginx:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#安裝pcre支持rewrite庫,也可以安裝源碼,注*安裝源碼時,指定pcre路徑爲解壓源碼的路徑,而不是編譯後的路徑,否則會報錯。
yum install pcre-devel pcre -y
#下載Nginx源碼包
cd /usr/src ;wget -c http://nginx.org/download/nginx-1.2.6.tar.gz
#解壓Nginx源碼包
tar -xzf nginx-1.2.6.tar.gz
#進入解壓目錄,然後sed修改Nginx版本信息爲TDTWS
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
#預編譯Nginx
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#.configure預編譯成功後,執行make命令進行編譯
make
#make執行成功後,執行make install 正式安裝
make install
#自此Nginx安裝完畢!!!

三、配置Nginx:

  這裏鑑於我的51CTO博客已經有Tomcat安裝和配置了,這裏忽略,只配置Nginx。

#進入Nginx應用目錄
cd /usr/local/nginx/conf
#備份原nginx.conf文件
mv  nginx.conf  nginx.bak
創建 vi nginx.conf ,並寫入如下內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
user www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
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;
###2012-12-19 change nginx logs
log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent"  $request_time $remote_addr';
#這裏爲後端服務器wugk應用集羣配置,根據後端實際情況修改即可,tdt_wugk爲負載均衡名稱,可以任意指定
#但必須跟vhosts.conf虛擬主機的pass段一致,否則不能轉發後端的請求。
upstream tdt_wugk {
    server   10.10.141.30:8080 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.30:8081 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.31:8080 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.31:8081 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.32:8080 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.32:8081 weight=1max_fails=2fail_timeout=30s;
}
#這裏爲後端APP應用負載均衡配置,根據後端實際情況修改即可。tdt_app爲負載均衡名稱,可以任意指定
upstream tdt_app {
    server   10.10.141.40:8080 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.40:8081 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.41:8080 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.41:8081 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.42:8080 weight=1max_fails=2fail_timeout=30s;
    server   10.10.141.42:8081 weight=1max_fails=2fail_timeout=30s;
}
#include引用vhosts.conf,該文件主要用於配置Nginx 虛擬主機
include vhosts.conf;
}

  如上nginx.conf配置完畢,繼續配置nginx虛擬主機,繼續在當前目錄創建vhosts.conf

  vi vhosts.conf 內容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
####www.wuguangke.cn
server
  {
    listen       80;
    server_name  www.wuguangke.cn;
    index index.html index.htm;
#配置發佈目錄爲/data/www/wugk
    root  /data/www/wugk;
    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://tdt_wugk;
         expires      3d;
    }
#動態頁面交給http://tdt_wugk,也即我們之前在nginx.conf定義的upstream tdt_wugk 均衡
    location ~ .*\.(php|jsp|cgi)?$
    {
         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://tdt_wugk;
    }
#配置Nginx動靜分離,定義的靜態頁面直接從Nginx發佈目錄讀取。
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
    {
    root /data/www/wugk;
    #expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力
    expires      3d;
    }
#定義Nginx輸出日誌的路徑
    access_log  /data/logs/nginx_wugk/access.log main;
    error_log   /data/logs/nginx_wugk/error.log  crit;
}
##########chinaapp.sinaapp.com 2012-12-19
  server
  {
    listen       80;
    server_name  chinaapp.sinaapp.com;
    index index.html index.htm;
    root  /data/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://tdt_app;
         expires      3d;
    }
    location ~ .*\.(php|jsp|cgi)?$
    {
         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://tdt_app;
    }
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
    {
    root /data/www/app;
    expires      3d;
    }
    access_log  /data/logs/nginx_app/access.log main;
    error_log   /data/logs/nginx_app/error.log  crit;
}

四、部署測試:

  後端配置好Tomcat服務,並啓動,發佈的程序需同步到Nginx的/data/www對應的目錄,因爲配置動靜分離後,用戶請求你定義的靜態頁面,默認會去nginx的發佈目錄請求,而不會到後端請求,所以這時候你要保證後端跟前端的程序保持一致,可以使用Rsync做服務端自動同步或者使用NFS、MFS分佈式共享存儲。

1
2
3
4
5
6
7
8
#檢查Nginx配置文件是否配置正確,提示Ok and successful表示正確,如下:
[root@WEB-11-151 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#啓動Nginx服務
/usr/local/nginx/sbin/nginx
#查看Nginx進程是否啓動
ps -ef |grep nginx

 本文只是一個簡單的實際案例,裏面的配置和參數這裏沒有做過多的說明,後期會繼續更新

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章