1. 程式人生 > >nginx 動靜分離

nginx 動靜分離

動靜分離

隨著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:

  1. #安裝pcre支持rewrite庫,也可以安裝源碼,註*安裝源碼時,指定pcre路徑為解壓源碼的路徑,而不是編譯後的路徑,否則會報錯。

  2. yum install pcre-devel pcre -y

  3. #下載Nginx源碼包

  4. cd /usr/src ;wget -c http://nginx.org/download/nginx-1.2.6.tar.gz

  5. #解壓Nginx源碼包

  6. tar -xzf nginx-1.2.6.tar.gz

  7. #進入解壓目錄,然後sed修改Nginx版本信息為TDTWS

  8. 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

  9. #預編譯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。

  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. worker_rlimit_nofile 102400;

  6. events

  7. {

  8. use epoll;

  9. 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 內容如下:

  1. ####www.wuguangke.cn

  2. server

  3. {

  4. listen 80;

  5. server_name www.wuguangke.cn;

  6. index index.html index.htm;

  7. #配置發布目錄為/data/www/wugk

  8. root /data/www/wugk;

  9. 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. [[email protected] ~]# /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

本文出自 “梁明月” 博客,謝絕轉載!

nginx 動靜分離