大网数据广告活动
服务器运维问答

mod_rewrite issue Apache behind Nginx

· 52CE 我爱测

I have nginx proxying to apache as a backend in order for nginx to handle static assets. Unfortunately when I attempt to visit a "pretty url" it just displays the index page. nginx config server { listen 80; root /var/www/example…

问题原文

原帖链接 · Titan · CC BY-SA 3.0

I have nginx proxying to apache as a backend in order for nginx to handle static assets. Unfortunately when I attempt to visit a "pretty url" it just displays the index page.

nginx config

server {
        listen   80;

        root /var/www/example.com;
        index index.php index.html index.htm;

        server_name example.com www.example.com a.example.com b.example.com c.example.com;

        location / {
                try_files $uri $uri/ /index.php;
        }

        location ~ \.php$ {

                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8080;
        }

         location ~ /\.ht {
                deny all;
        }
}

Apache vhost config

<VirtualHost 127.0.0.1:8080>
        ServerName example.com
        ServerAlias www.example.com
        ServerAlias a.example.com
        ServerAlias b.example.com
        ServerAlias c.example.com

        ServerAdmin [email omitted]
        DocumentRoot /var/www/example.com

        <Directory /var/www/example.com>
                AllowOverride All
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/5r_error.log
        CustomLog ${APACHE_LOG_DIR}/5r_access.log combined
</VirtualHost>

.htaccess

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REQUEST_FILENAME} !/maintenance.html
RewriteRule ^.*$ /maintenance.html [L]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^example-old.co.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example-old.co.uk [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteRule ^servers/([^\/]+)/?$ servers.php?scroll-to-server=$1 [L,QSA]
RewriteRule ^media/([0-9]+)$ media.php?media-id=$1 [L,QSA]
RewriteRule ^media/([a-z,]+)$ media.php?filter=$1 [L,QSA]
RewriteRule ^manage/?([^\/]+)$ admin.php?page=$1 [L,QSA]
RewriteRule ^operations/?([^/]*)(.*)$ operations.php?c=$1&m=$2 [L,QSA]

ErrorDocument 404 /error
ErrorDocument 403 /error

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]

When I visit example.com it loads fine, when I visit example.com/test it just shows the homepage still. The request in chrome for the page returns 200.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

Your try_files changes the URL to /index.php in order to send it upstream which looses the pretty permalink.

You might try to send the pretty permalink upstream by adding a named location. Maybe:

location / {
    try_files $uri $uri/ @proxy;
}

location @proxy {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
}
location ~ \.php$ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080;
}

See this and this for details.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。