Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

justl00king's avatar

Http to Https page isn't redirecting properly

Here is my nginx config. It's not working, I get an error in the browser "page isn't redirecting properly". Am I doing something wrong or missing something that's causing this?

server {
    listen 80;
    listen [::]:80;
    server_name mydomain.com www.mydomain.com;
    return 301 https://$server_name$request_uri;

}

server {    
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    
    server_name mydomain.com www.mydomain.com;
    return 301 $scheme://$server_name$request_uri;
}

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;

    server_name mydomain.com www.mydomain.com;
    root /home/forge/mydomain.com/public;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!3DES';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    #add_header X-Frame-Options: "ALLOW-FROM https://www.facebook.com/";
    add_header X-XSS-Protection "1; mode=block"; 
    add_header X-Content-Type-Options "nosniff";
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
    # add_header Content-Security-Policy "";

    client_max_body_size 200M;

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DOT NOT REMOVE!)
    include forge-conf/ mydomain.com/server/*;

    # Get users true ip address
    set_real_ip_from 10.0.0.0/16;
    real_ip_header X-Forwarded-For;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/mydomain.com-error.log error;

    error_page 404 /index.php;


    location ~ \.php$ {
        
        fastcgi_cache phpcache; # The name of the cache key-zone to use
        fastcgi_cache_valid 200 30m; # What to cache: 'Code 200' responses, for half an hour
        fastcgi_cache_methods GET HEAD; # What to cache: only GET and HEAD requests (not POST)
        add_header X-Fastcgi-Cache $upstream_cache_status; # Add header so we can see if the cache hits or misses
        
        # try_files $uri 404;
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    # Enable gzip compression.
    # Default: off
    gzip on;

    # Compression level (1-9).
    # 5 is a perfect compromise between size and CPU usage, offering about
    # 75% reduction for most ASCII files (almost identical to level 9).
    # Default: 1
    gzip_comp_level    5;

    # Don't compress anything that's already small and unlikely to shrink much
    # if at all (the default is 20 bytes, which is bad as that usually leads to
    # larger files after gzipping).
    # Default: 20
    gzip_min_length    256;

    # Compress data even for clients that are connecting to us via proxies,
    # identified by the "Via" header (required for CloudFront).
    # Default: off
    gzip_proxied       any;

    # Tell proxies to cache both the gzipped and regular version of a resource
    # whenever the client's Accept-Encoding capabilities header varies;
    # Avoids the issue where a non-gzip capable client (which is extremely rare
    # today) would display gibberish if their proxy gave them the gzipped version.
    # Default: off
    gzip_vary          on;

    # Compress all output labeled with one of the following MIME-types.
    # text/html is always compressed by gzip module.
    # Default: text/html
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;

}
0 likes
6 replies
xLukas's avatar

I guess you want to redirect to https://your-domain.com (non-www). Where I see possible error is second and third server block. Basically in second block you're catching https request with and without www and redirect them to https without www which in second loop catch the second server block because you set it's server name also to listen (non-www) variant. Your block should be like this

server {
    listen 80;
    listen [::]:80;
    server_name mydomain.com www.mydomain.com;
    return 301 https://$server_name$request_uri;

}

server {    
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    
    server_name www.mydomain.com;
    return 301 $scheme://$server_name$request_uri;
}

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;

    server_name mydomain.com;
    root /home/forge/mydomain.com/public;

    .....

I delete non-www version from second server block and www-version from third server block. Try it if it works.

justl00king's avatar

@xLukas, thank you. Unfortunately I'm still getting the same errors.

Chrome says "this page isn't working, redirected to many times"

Firefox says "The page isn’t redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Any other ideas? Thanks!

xLukas's avatar

Did you reload your server after changing config? nginx -s reload Tip: if you want to check if syntax in your nginx config file is right run nginx -t

justl00king's avatar

@xLukas, yes I restarted nginx and the server.

When I ran nginx -t as you suggested, I get this:

nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1 nginx: [warn] conflicting server name "www.mydomain.com" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "www.mydomain.com" on [::]:80, ignored nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: [emerg] open() "/run/nginx.pid" failed (13: Permission denied) nginx: configuration file /etc/nginx/nginx.conf test failed

I ran sudo nginx -t and get:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

xLukas's avatar

I think the 301 redirect response is cached by your browser. Try to clear your browser's cache.

Please or to participate in this conversation.