Nginx Server配置如下(测试环境):
server {
listen 80;
server_name server110.com;
location / {
root /home/www/server110.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_passhttp://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端口。