Nginx Remove WWW And Respond To Both

前端 未结 6 1819
粉色の甜心
粉色の甜心 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:27

    On the first question - simply add both domains:

    server_name mydomain.io www.mydomain.io;
    

    For the second, you'll need this simple redirect:

    server {
          listen 80;
    
          server_name www.mydomain.io mydomain.io;
    
          if ($host = 'www.mydomain.io' ) {
             rewrite  ^/(.*)$  http://mydomain.io/$1  permanent;
          }
    

提交回复
热议问题