Nginx删除WWW并同时回应 [英] Nginx Remove WWW And Respond To Both

查看:66
本文介绍了Nginx删除WWW并同时回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下nginx配置片段:

I have the following nginx configuration fragment:

server {
   listen 80;

   server_name mydomain.io;

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

   index index.html index.php;

   access_log /var/log/nginx/domains/mydomain.io/access.log;
   error_log /var/log/nginx/domains/mydomain.io/error.log;

   location ~\.php {
      try_files $uri =404;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_intercept_errors on;
      fastcgi_pass 127.0.0.1:9000;
      include /etc/nginx/fastcgi_params;
   }
}

首先,如何使服务器块响应both http://www.mydomain.io 以及 http://mydomain.io .其次,如果它们来自 http://www.mydomain.io ,我想强制将其重定向到http://mydomain.io .

First, how can I make the server block respond to both http://www.mydomain.io and also http://mydomain.io. Second, I want to force if they come from http://www.mydomain.io to redirect to http://mydomain.io.

谢谢.

推荐答案

关于第一个问题-只需添加两个域:

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;
      }

这篇关于Nginx删除WWW并同时回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆