Join the community today
Become a Member

redirecting to www and https

Discussion in 'Forum software usage' started by maria, Mar 1, 2015.

  1. maria

    maria New Member

    10
    2
    3
    Mar 1, 2015
    Ratings:
    +2
    Local Time:
    1:36 PM
    5.5
    Hello,

    I want to redirect to www and htttps, would this code be correct to add to the config?

    Code:
    server {
    listen 80;
    server_name mydomain.com;
    # add ssl settings
    return 301 https://www.mydomain.com$request_uri;
    }
    When I add this i get this error when restarting nginx

    Code:
    nginx: [emerg] "location" directive is not allowed here in /usr/local/nginx/conf/pagespeedhandler.conf:3
     
  2. eva2000

    eva2000 Administrator Staff Member

    29,028
    6,588
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,780
    Local Time:
    3:36 AM
    Nginx 1.13.x
    MariaDB 5.5
    what does full nginx vhost look like ?
     
  3. maria

    maria New Member

    10
    2
    3
    Mar 1, 2015
    Ratings:
    +2
    Local Time:
    1:36 PM
    5.5
    Thanks for your reply.
    Code:
    # https SSL SPDY vhost
    server {
            listen 443 ssl spdy;
                server_name testdomain.com;
    
            ssl_dhparam /usr/local/nginx/conf/ssl/testdomain.com/dhparam.pem;
            ssl_certificate      /usr/local/nginx/conf/ssl/testdomain.com/test.crt;
            ssl_certificate_key  /usr/local/nginx/conf/ssl/testdomain.com/test.key;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_session_cache      shared:SSL:10m;
            ssl_session_timeout  10m;
            ssl_ciphers ************
            ssl_prefer_server_ciphers   on;
            add_header Alternate-Protocol  443:npn-spdy/3;
            add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
            #add_header  X-Content-Type-Options "nosniff";
            #add_header X-Frame-Options DENY;
            # nginx 1.5.9+ or higher
            # http://nginx.org/en/docs/http/ngx_http_spdy_module.html#spdy_headers_comp
            # http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_buffer_size
            # spdy_headers_comp 0;
            # ssl_buffer_size 4k;
    
            # enable ocsp stapling
            resolver 8.8.8.8;
            ssl_stapling on;
            ssl_stapling_verify on;
            ssl_trusted_certificate /usr/local/nginx/conf/ssl/testdomain.com/test.crt;
    
    # ngx_pagespeed & ngx_pagespeed handler
    include /usr/local/nginx/conf/pagespeed.conf;
    include /usr/local/nginx/conf/pagespeedhandler.conf;
    include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
    
       ##redirect https://non-www to https://www
    ##redirect http:// to https://
          if ($host = ‘testdomain.com' ) {
            return 302 https://www.$server_name$request_uri;
          }
    
      # limit_conn limit_per_ip 16;
      # ssi on;
    
      access_log /home/nginx/domains/testdomain.com/log/access.log combined buffer=32k;
      error_log /home/nginx/domains/testdomain.com/log/error.log;
    
      root /home/nginx/domains/testdomain.com/public;
    
      location /forum/ {
    
      # block common exploits, sql injections etc
      #include /usr/local/nginx/conf/block.conf;
    
      index index.php index.html index.htm;
      try_files $uri $uri/ /forum/index.php?$uri&$args;
    
      }
    
            location /forum/internal_data/ {
            internal;
            allow 127.0.0.1;
            deny all;
            }
    
            location /forum/library/ {
            internal;
            allow 127.0.0.1;
            deny all;
            }
    
            location /forum/data/taigachat/ {
            open_file_cache off;
            include /usr/local/nginx/conf/staticfiles.conf;
    }
    
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
    }
    
    I have https fixed so it always redirects to https, but I can not figure out how to make it so that the naked domain is redirected to www.
     
  4. eva2000

    eva2000 Administrator Staff Member

    29,028
    6,588
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,780
    Local Time:
    3:36 AM
    Nginx 1.13.x
    MariaDB 5.5
    default vhost has commented out example of how to do that just change $scheme to https
    Code:
    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    server {
                listen   80;
                server_name newdomain.com;
                return 301 https://www.newdomain.com$request_uri;
           }
    
    server {
            listen 443 ssl spdy;
                server_name www.newdomain.com;
    
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
    rest of your normal nginx vhost below
    
     
  5. maria

    maria New Member

    10
    2
    3
    Mar 1, 2015
    Ratings:
    +2
    Local Time:
    1:36 PM
    5.5
    Hello, I added that and everything works but I get alot of redirects, is there any way to reduce them?
     
  6. eva2000

    eva2000 Administrator Staff Member

    29,028
    6,588
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,780
    Local Time:
    3:36 AM
    Nginx 1.13.x
    MariaDB 5.5
    redirects for what and where to ?
     
  7. maria

    maria New Member

    10
    2
    3
    Mar 1, 2015
    Ratings:
    +2
    Local Time:
    1:36 PM
    5.5
  8. eva2000

    eva2000 Administrator Staff Member

    29,028
    6,588
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,780
    Local Time:
    3:36 AM
    Nginx 1.13.x
    MariaDB 5.5
    above example redirects in post 4, should only redirect from

    Code:
    http://test.net
    to

    Code:
    https://www.test.net
    there's no way it would redirect to your forum url by itself unless you are telling your site to do so via nginx vhost redirects.
     
  9. maria

    maria New Member

    10
    2
    3
    Mar 1, 2015
    Ratings:
    +2
    Local Time:
    1:36 PM
    5.5
    that is intentional, I don't even know why I created a forum folder when i have nothing else hosted on my site, I could have just put it in the home folder.

    So there is no way for it to redirect straight from

    test.net

    to

    https://www.test.net/forum/

    without going through all those redirect steps?

    You said that test.net should redirect straight to https://www.test.net but for me it goes through an extra step:

    test.net

    https://test.net

    https://www.test.net

    Is there any way to remove the redirect in the middle?
     
  10. eva2000

    eva2000 Administrator Staff Member

    29,028
    6,588
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,780
    Local Time:
    3:36 AM
    Nginx 1.13.x
    MariaDB 5.5
    you need a live domain for me to see what's going on and where are you viewing these redirects in browser ?
     
  11. AlekseY

    AlekseY Member

    47
    4
    8
    Feb 18, 2015
    Ratings:
    +7
    Local Time:
    8:36 PM
    You can try
    #nginx.conf
    Code:
    map $scheme:$host $redirect {
         #~^https:www.(.*)            1;    
         ~^http:(.*)             1;
                        default        0;                     
    }