CentOS Nginx反向代理 + Apache配置

Nginx處理靜態內容是把好手,Apache雖然佔用內存多了點,性能上稍遜,但一直比較穩健。卻是Nginx的FastCGI有時候會出現502 Bad Gateway錯誤。一個可選的方法是Nginx作前端代理,處理靜態內容,動態請求通通轉發給後端Apache。Nginx Server配置以下(測試環境):php

server {
   listen 80;
   server_name digicake.com;

   location / {
      root /home/www/digicake.com/www;
      index index.php index.html;

      # Nginx找不到文件時,轉發請求給後端Apache
      error_page 404 @proxy;

      # css, js 靜態文件設置有效期1天
      location ~ .*\.(js|css)$ {
         access_log off;
         expires      1d;
      }

      # 圖片設置有效期3天
      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
         access_log off;
         expires      3d;
      }
   }

   # 動態文件.php請求轉發給後端Apache
   location ~ \.php$ {
     #proxy_redirect off;
     #proxy_pass_header Set-Cookie;
     #proxy_set_header Cookie $http_cookie;

      # 傳遞真實IP到後端
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass   http://127.0.0.1:8080;
   }

   location @proxy {
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://127.0.0.1:8080;
   }
}

環境:CentOS6.0-64bit,Nginx 1.0.8,Apache 2.2.21,PHP 5.3.8。其中Nginx全面接管80端口,Apache退居二線,監聽8080端口。 css

相關文章
相關標籤/搜索