Nginx no-www to www and www to no-www

前端 未结 17 2209
予麋鹿
予麋鹿 2020-11-22 09:53

I am using nginx on Rackspace cloud following a tutorial and having searched the net and so far can\'t get this sorted.

I want www.mysite.com to go to mysite.com as

17条回答
  •  难免孤独
    2020-11-22 10:05

    Actually you don't even need a rewrite.

    server {
        #listen 80 is default
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }
    
    server {
        #listen 80 is default
        server_name example.com;
        ## here goes the rest of your conf...
    }
    

    As my answer is getting more and more up votes but the above as well. You should never use a rewrite in this context. Why? Because nginx has to process and start a search. If you use return (which should be available in any nginx version) it directly stops execution. This is preferred in any context.

    Redirect both, non-SSL and SSL to their non-www counterpart:

    server {
        listen               80;
        listen               443 ssl;
        server_name          www.example.com;
        ssl_certificate      path/to/cert;
        ssl_certificate_key  path/to/key;
    
        return 301 $scheme://example.com$request_uri;
    }
    
    server {
        listen               80;
        listen               443 ssl;
        server_name          example.com;
        ssl_certificate      path/to/cert;
        ssl_certificate_key  path/to/key;
    
        # rest goes here...
    }
    

    The $scheme variable will only contain http if your server is only listening on port 80 (default) and the listen option does not contain the ssl keyword. Not using the variable will not gain you any performance.

    Note that you need even more server blocks if you use HSTS, because the HSTS headers should not be sent over non-encrypted connections. Hence, you need unencrypted server blocks with redirects and encrypted server blocks with redirects and HSTS headers.

    Redirect everything to SSL (personal config on UNIX with IPv4, IPv6, SPDY, ...):

    #
    # Redirect all www to non-www
    #
    server {
        server_name          www.example.com;
        ssl_certificate      ssl/example.com/crt;
        ssl_certificate_key  ssl/example.com/key;
        listen               *:80;
        listen               *:443 ssl spdy;
        listen               [::]:80 ipv6only=on;
        listen               [::]:443 ssl spdy ipv6only=on;
    
        return 301 https://example.com$request_uri;
    }
    
    #
    # Redirect all non-encrypted to encrypted
    #
    server {
        server_name          example.com;
        listen               *:80;
        listen               [::]:80;
    
        return 301 https://example.com$request_uri;
    }
    
    #
    # There we go!
    #
    server {
        server_name          example.com;
        ssl_certificate      ssl/example.com/crt;
        ssl_certificate_key  ssl/example.com/key;
        listen               *:443 ssl spdy;
        listen               [::]:443 ssl spdy;
    
        # rest goes here...
    }
    

    I guess you can imagine other compounds with this pattern now by yourself.

    More of my configs? Go here and here.

提交回复
热议问题