Ngingx redirect issues

Technical Support
  • Brand new install. Having trouble getting www to redirect to non www.

    http://mywebsite.com redirects to https://www.mywebsite.com # Notice http to https works, but its sending me to www
    https://mywebsite.com redirects to https://www.mywebsite.com # Sends me to www

    http://mywebsite.com/forum/ redirects to https://mywebsite.com/forum #which is ok and works as it should
    https://www.mywebsite.com/forum/ does not redirect to non http and gives me a messed up looking forum, cant find any icons, etc

                                                                   
    server {
    
        server_name www.mywebsite.com mywebsite.com;  # notice no subfolder defined here
        root /var/www/html;
    
        location / {
            #try_files $uri $uri/ =404;
            try_files $uri /index.php$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
    
        location ~ /\.ht {
            deny all;
        }
    
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }
    
        location /forum/ {  # but it is defined here
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
    
            proxy_pass http://127.0.0.1:4567;  # no subfolder defined here
            proxy_redirect off;
    
            # Socket.IO Support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    
    }
    server {
        if ($host = www.mywebsite.com) {
            return 301 https://mywebsite.com$request_uri;
        } # managed by Certbot
    
    
        if ($host = mywebsite.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        listen 80;
    
        server_name www.mywebsite.com mywebsite.com;
        return 404; # managed by Certbot
    
    
    
    
    
    
  • Any particular reason you don't want your normal site to also be at https://mywebsite.com, redirecting www.mywebsite.com to there?

  • That is what I want, I am sorry if I explained it wrong. My whole goal is to end up with http://mywebsite.com for my regular site and https://mywebsite.com/forum for nodebb. I dont want any www in there at all

  • What you're going to want is two server blocks. One with only server_name mywebsite.com;, with your normal site and NodeBB stuff, with SSL. The other with server_name www.mywebsite.com; that only has a return 301 https://mywebsite.com$request_uri;

  • You have only 80 redirection but,
    you need also 443 redirection:

    server {                                                                                                                                                               
        listen 443 ssl http2;                                                                                                                                              
        server_name www.mywebsite.com;                                                                                                                                     
        include /usr/local/nginx/conf/ssl.conf;                                                                                                             
        root /home/user/webapps/nodebb/;                                                                                                                            
        return 301 https://mywebsite.com$request_uri;                                                                                                                      
    }
    

    You shouldnt' use if's for redirection only server blocks.

    server_name www.mywebsite.com mywebsite.com;
    

    Get rid of www.mywebsite.com from the config.

  • This post is deleted!
  • This is the final config that works. See any issues?

    server {
        server_name www.mywebsite.com;
        return 302 https://mywebsite.com$request_uri;
    }
    
    server {
        listen 443 ssl http2;
        server_name mywebsite.com;  # notice no subfolder defined here
        root /var/www/html;
        index index.php;
    
            
        ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
    
        location ~ /\.ht {
            deny all;
        }
    
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }
    
        location /forum/ {  # but it is defined here
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
    
            proxy_pass http://127.0.0.1:4567;  # no subfolder defined here
            proxy_redirect off;
    
            # Socket.IO Support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    
    }
    
    
    server {                                                                                                                                                               
        listen 443 ssl http2;                                                                                                                                              
        server_name www.mywebsite.com;                                                                                                                                     
        return 302 https://mywebsite.com$request_uri;                                                                                                                      
    }
    
  • Does it work? I don't see any issues.

    just maybe location /forum should be without slash and server block should have listen 80; directive.

    and don't forget about assets and plugins locations.

  • @MoJo It does work, except for the admin area, but i have asked that in a seperate post.

    I didnt see anything in the docs about assets and plugins. Ill go look again.


Suggested Topics