CentOS Nginx反向代理 + Apache配置
Nginx处理静态内容是把好手,Apache虽然占用内存多了点,性能上稍逊,但一直比较稳健。倒是Nginx的FastCGI有时候会出现502 Bad Gateway错误。一个可选的方法是Nginx做前端代理,处理静态内容,动态请求统统转发给后端Apache。Nginx Server配置如下(测试环境): 改掉整个配置文件,文件位置
1vi /etc/nginx/conf.d/default.conf
2
1server {
2 listen 80;
3 server_name digicake.com; # Domain name
4
5 location / {
6 root /home/www/digicake.com/www; # route
7 index index.php index.html;
8
9 # Nginx找不到文件时,转发请求给后端Apache
10 error_page 404 @proxy;
11
12 # css, js 静态文件设置有效期1天
13 location ~ .*\.(js|css)$ {
14 access_log off;
15 expires 1d;
16 }
17
18 # 图片设置有效期3天
19 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
20 access_log off;
21 expires 3d;
22 }
23 }
24
25 # 动态文件.php请求转发给后端Apache
26 location ~ \.php$ {
27 #proxy_redirect off;
28 #proxy_pass_header Set-Cookie;
29 #proxy_set_header Cookie $http_cookie;
30
31 # 传递真实IP到后端
32 proxy_set_header Host $http_host;
33 proxy_set_header X-Real-IP $remote_addr;
34 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
35
36 proxy_pass http://127.0.0.1:8080; # 确认apache的端口
37 }
38
39 location @proxy {
40 proxy_set_header Host $http_host;
41 proxy_set_header X-Real-IP $remote_addr;
42 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
43
44 proxy_pass http://127.0.0.1:8080; # 确认apache的端口
45 }
46}
47
这样当找不到文件就会转到用apache,但如果在nginx文件夹里有对应文件,优先访问的还是nginx
评论 (0)