1. 程式人生 > >Nginx動靜分離+快取

Nginx動靜分離+快取

Nginx動靜分離+快取

#進入Nginx應用目錄 
cd /etc/nginx/

#備份原nginx.conf檔案 
mv  nginx.conf  nginx.bak  

建立 vi nginx.conf ,並寫入如下內容:
user www www; 
worker_processes 8; 
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; 
pid /run/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; 
# 該指令設定與upstream server的連線超時時間,有必要記住,這個超時不能超過75秒
  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=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.30:8081 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.31:8080 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.31:8081 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.32:8080 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.32:8081 weight=1 max_fails=2 fail_timeout=30s; 
} 
#這裡為後端APP應用負載均衡配置,根據後端實際情況修改即可。tdt_app為負載均衡名稱,可以任意指定
upstream tdt_app { 
    server   10.10.141.40:8080 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.40:8081 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.41:8080 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.41:8081 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.42:8080 weight=1 max_fails=2 fail_timeout=30s; 
    server   10.10.141.42:8081 weight=1 max_fails=2 fail_timeout=30s; 
} 
#include引用vhosts.conf,該檔案主要用於配置Nginx 虛擬主機  
include vhosts.conf; 
} 
   如上nginx.conf配置完畢,繼續配置nginx虛擬主機,繼續在當前目錄建立vhosts.conf

  vi vhosts.conf 內容如下:
####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; #反向代理(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; 
} 
#靜態的檔案(圖片、js、CSS等)
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
    { 
    root /data/www/app; 
    expires      3d; 
    } 
     #定義Nginx輸出日誌的路徑 
    access_log  /data/logs/nginx_app/access.log main; 
    error_log   /data/logs/nginx_app/error.log  crit; 
}