Nginx redirect loop

7,588

I think your problem is this bit of the configuration:

    if ($host = 'mysitehere.com') {
        rewrite ^/(.*)$ http://mysitehere.com/$1 last;
    }

Note that this is basically rewriting any URL at mysitehere.com to itself, and the last directive stops further processing. Because the rewritten URL starts with http://, it's instructing Nginx to do an external 301 redirect rather than internally rewrite the URL and serve the page as if it were another URL. I'd just remove this section entirely.

If you haven't done so, check out the Nginx rewrite documentation for details; it's not that complicated to read.

Share:
7,588

Related videos on Youtube

user57187
Author by

user57187

Updated on September 18, 2022

Comments

  • user57187
    user57187 over 1 year

    I am trying to transfer a site from one server to another. I normally use apache, but this project was using nginx and Ultimate SEO for url modification. I am using the same nginx config file but I am ending up with a redirect loop error. Here are the contents of the default file in 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;
    
    
    }
    

    I replaced the actual url with "mysitehere", but otherwise, it's the same. I tried reading through other posts regarding this matter, but could not identify what was causing the error in this particular file. Does anyone have any ideas what is causing this?

  • ServerChecker
    ServerChecker about 7 years
    The Nginx link is broken