From:http://my.oschina.net/HankCN/blog/168601
Nginx处理静态内容是把好手,Apache虽然占用内存多了点,性能上稍逊,但一直比较稳健。倒是Nginx的FastCGI有时候会出现502 Bad Gateway错误。一个可选的方法是Nginx做前端代理,处理静态内容,动态请求统统转发给后端Apache。Nginx Server配置如下(测试环境):
改掉整个配置文件,文件位置
vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name digicake.com; # Domain name
location / {
root /home/www/digicake.com/www; # route
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; # 确认apache的端口
}
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; # 确认apache的端口
}
}
这样当找不到文件就会转到用apache,但如果在nginx文件夹里有对应文件,优先访问的还是nginx