Nginx Remove WWW And Respond To Both

前端 未结 6 1809
粉色の甜心
粉色の甜心 2020-12-29 21:53

I have the following nginx configuration fragment:

server {
   listen 80;

   server_name mydomain.io;

   root /srv/www/domains/mydomain.io;

   index index         


        
6条回答
  •  旧巷少年郎
    2020-12-29 22:33

    I believe it's better to add two seperate server blocks to avoid unnecessary checking by the if block. I also use the $scheme variable so that HTTPS requests will not be redirected to their insecure counterparts.

    server {
        listen 80;
    
        server_name www.mydomain.io;
    
        rewrite ^ $scheme://mydomain.io$uri permanent;
    }
    
    server {
        listen 80;
    
        server_name mydomain.io;
    
        # your normal server block definitions here
    }
    

提交回复
热议问题