php - nginx反向代理apache动静分离,路由不正常跳转

查看:151
本文介绍了php - nginx反向代理apache动静分离,路由不正常跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

运行环境: centos7 php7.1 nginx 1.10 apache 2.4 php框架phalcon

nginx.conf配置

upstream lamp{
        server 127.0.0.1:81 weight=1 max_fails=3 fail_timeout=30s;
    }
server {
    listen       80;
    server_name  we.test.com;
    index index.php
    root         /wwwroot/test/public;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ .*\.(php|jsp|cgi)?$
    {
        proxy_set_header Host  $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://lamp;
    }
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
    {
        root /wwwroot/test/public;
        expires      3d;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

apache vhost配置

<VirtualHost *:81>
    ServerName we.test.com
    DocumentRoot /wwwroot/test/public
    <Directory wwwwroot/test/public>
        Options FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        Allow from all
        Require All granted
    </Directory>
</VirtualHost>

.htaccess 文件 (在public目录下)

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

当我访问 we.test.com:81的时候所有的路由可以正常访问 例如 we.test.com/user/login
或者 接受 参数 we.test.com/article/index?id=5

但是当我 访问 nginx监听的80端口 ,所有的访问都很奇怪,就是浏览器的url会变成对应的路由 例如we.test.com/user/login 但是 页面展示的内容 还是 we.test.com的默认页,只是刷新了一下。

刚刚尝试这种 nginx 反向代理 apache ,不知道有没有大神可以帮忙解答一下。非常非常感谢。 如有有缺少的条件,我会及时回复 在线等。。。

解决方案

apache不需要更改,nginx配置改一下

location / {
                if (!-e $request_filename) {
                        rewrite ^/(.*)$ /index.php?$1 last;
                }
        }
        location ~ \.php$ {
         proxy_set_header Host  $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://lamp;
        }                               

这篇关于php - nginx反向代理apache动静分离,路由不正常跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆