Nginx + Apache 配置反向代理和靜態資源快取
阿新 • • 發佈:2019-01-10
Nginx處理靜態內容是把好手,Apache雖然佔用記憶體多了點,效能上稍遜,但一直比較穩健。倒是Nginx的FastCGI有時候會出現502 Bad Gateway錯誤。一個可選的方法是Nginx做前端代理,處理靜態內容,動態請求統統轉發給後端Apache。Nginx Server配置如下(測試環境):
server { listen 80; server_name test.com; location / { root /www/test; 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; } }