mod_rewrite 问题 Apache 落后于 Nginx

mod_rewrite 问题 Apache 落后于 Nginx

我将 nginx 代理到 apache 作为后端,以便 nginx 处理静态资产。不幸的是,当我尝试访问“漂亮的 URL”时,它只显示索引页。

nginx 配置

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 配置

<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 protected]
        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]

当我访问时,example.com它加载正常,当我访问时,example.com/test它仍然只显示主页。Chrome 中对该页面的请求返回 200。

答案1

try_files将 URL 更改为/index.php以便将其发送到上游,从而丢失漂亮 永久链接

您可以尝试发送漂亮 永久链接通过添加命名位置来向上游移动。可能:

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;
}

了解详情。

相关内容