![Nginx 重定向循环](https://linux22.com/image/547066/Nginx%20%E9%87%8D%E5%AE%9A%E5%90%91%E5%BE%AA%E7%8E%AF.png)
我正在尝试将网站从一台服务器转移到另一台服务器。我通常使用 apache,但这个项目使用 nginx 和 Ultimate SEO 进行 url 修改。我使用相同的 nginx 配置文件,但最终出现重定向循环错误。以下是 sites-available 中默认文件的内容:
# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts
server {
listen 80 default;
server_name mysitehere.com;
access_log /var/log/nginx/mysitehere.access.log;
#fastcgi_param DOCUMENT_ROOT /site/public_html/;
location / {
root /site/public_html/;
index index.php index.html index.htm;
# rewrite ^/(.*) http://mysitehere.com/$1 permanent;
#rewrite ^/index.php?main_page=shopping_cart$ http://mysitehere.com/index.php?main_page=shopping_cart last;
if ($host != 'mysitehere.com') {
rewrite ^/(.*)$ http://mysitehere.com/$1 last;
}
if ($host = 'mysitehere.com') {
rewrite ^/(.*)$ http://mysitehere.com/$1 last;
}
# From Ultimate SEO URLs
rewrite ^/(.*)-blog-(.*).html$ /index.php?main_page=blogread last;
rewrite ^/(.*)-se-(.*).html$ /index.php?main_page=readarticle last;
rewrite ^/(.*)-ca-(.*).html$ /index.php?main_page=readarticle last;
rewrite ^/blog.html$ /index.php?main_page=blog last;
rewrite ^/(.*)-p-(.*).html$ /index.php?main_page=product_info&products_id=$2&$query_string last;
rewrite ^/(.*)-ri-(.*).html$ /index.php?main_page=document_general_info&products_id=$2&$query_string last;
rewrite ^/(.*)-c-(.*).html$ /index.php?main_page=index&cPath=$2&$query_string} last;
rewrite ^/(.*)-m-([0-9]+).html$ /index.php?main_page=index&manufacturers_id=$2&$query_string last;
rewrite ^/(.*)-pi-([0-9]+).html$ /index.php?main_page=popup_image&pID=$2&$query_string last;
rewrite ^/(.*)-pr-([0-9]+).html$ /index.php?main_page=product_reviews&products_id=$2&$query_string last;
rewrite ^/(.*)-pri-([0-9]+).html$ /index.php?main_page=product_reviews_info&products_id=$2&$query_string last;
# For Open Operations Info Manager
rewrite ^/(.*)-i-([0-9]+).html$ /index.php?main_page=info_manager&pages_id=$2&$query_string last;
# For dreamscape's News & Articles Manager
rewrite ^/news/?$ /index.php?main_page=news&$query_string last;
rewrite ^/news/rss.xml$ /index.php?main_page=news_rss&$query_string last;
rewrite ^/news/archive/?$ /index.php?main_page=news_archive&$query_string last;
rewrite '^/news/([0-9]{4})-([0-9]{2})-([0-9]{2}).html$ /index.php?main_page=news&date=$1-$2-$3&$query_string' last;
rewrite '^/news/archive/([0-9]{4})-([0-9]{2}).html$ /index.php?main_page=news_archive&date=$1-$2&$query_string' last;
rewrite ^/news/(.*)-a-([0-9]+)-comments.html$ /index.php?main_page=news_comments&article_id=$2&$query_string last;
rewrite ^/news/(.*)-a-([0-9]+).html$ /index.php?main_page=news_article&article_id=$2&$query_string last;
# All other pages
if (!-f $request_filename) {
rewrite ^/(.*).html$ /index.php?main_page=$1&$query_string last;
}
location ~ /\. {
deny all;
}
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|bmp)$ {
root /site/public_html;
rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
rewrite ^(.*?)\.v-([^\.]+)\.(js|css)$ $1.$3;
access_log off;
expires max;
}
location ~* \.php$ {
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param DOCUMENT_ROOT /site/public_html;
fastcgi_param SCRIPT_FILENAME /site/public_html;
include fastcgi_params;
}
# HTTPS
error_page 404 /index.php?main_page=page_not_found;
}
我用“mysitehere”替换了实际的 URL,但除此之外,它还是一样。我尝试阅读有关此事的其他帖子,但无法确定是什么导致了此特定文件的错误。有人知道是什么原因造成的吗?
答案1
我认为你的问题在于这个配置部分:
if ($host = 'mysitehere.com') {
rewrite ^/(.*)$ http://mysitehere.com/$1 last;
}
请注意,这基本上是将 mysitehere.com 上的任何 URL 重写为其自身,最后一个指令停止进一步处理。由于重写的 URL 以 http:// 开头,因此它指示 Nginx 执行外部 301 重定向,而不是内部重写 URL 并将页面作为另一个 URL 提供。我会完全删除此部分。
如果你还没有这样做,请查看Nginx 重写文档了解详细信息;阅读起来并不复杂。