【问题标题】:www in domain not working in nginx域中的 www 在 nginx 中不起作用
【发布时间】:2012-04-14 15:52:06
【问题描述】:

我是使用 nginx 的新手,嗯,是使用任何非 cpanel 的新手...当您包含 www 时,我在使用 nginx 让域工作时遇到问题。在网址中。

www.mydomain.com > not work 404
mydomain.com > works

我不确定我是否在命名配置文件或 nginx 的服务器配置方面犯了错误。我有点急于学习,如果我在基本配置上犯了一些错误,我不会感到惊讶。我运行最新的 nginx 和 php-fpm,除了我的域问题之外它可以工作。

我正在(尝试?)运行子域,它们可以工作,但使用 www。将导致 404。我使用主 .org 服务器域中的名称服务器等。 我将在下面发布所有相关内容,希望这里的人能够发现我正在犯/或犯的错误。

etc/hosts 
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1       localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
184.xxx.xxx.146 server.servername.org servername.org 

named.conf

   ... 
   view "localhost_resolver" {
/* This view sets up named to be a localhost resolver ( caching only nameserver ).
 * If all you want is a caching-only nameserver, then you need only define this view:
 */
   # match-clients         { 127.0.0.0/24; };
   # match-destinations    { localhost; };
   match-clients      { any; };
   match-destinations { any; };
   recursion no;

        zone "servername.org" {
                type master;
                file "/var/named/servername.org.db";
        };

// optional - we act as the slave (secondary) for the delegated domain
zone "mydomain.com" IN {
  type slave;
  file "/var/named/mydomain.com.db";
  masters {10.10.0.24;};
}; 
allow-notify { 184.xxx.xxx.146; };
};

mydomain.com.db

$TTL    86400
mydomain.com.  IN      SOA     ns1.servername.org.      server.servername.org.        (
                                2002012013; Serial
                                1H      ; Refresh (change 1H to 6H in 3 days or so)
                                1800    ; Retry (change to 1H in 3 days)
                                2W      ; Expire
                                1D ); Minimum
mydomain.com.          IN      NS      ns1.servername.org.
mydomain.com.          IN      NS      ns2.servername.org.
ns1.servername.org.              IN      A       184.xxx.xxx.147
ns2.servername.org.             IN      A       184.xxx.xxx.148
mail.servername.org.             IN      A       184.xxx.xxx.146
mydomain.com.          IN      A       184.xxx.xxx.146
mydomain.com.          IN      MX      0       mail.servername.org.
@                               A       184.xxx.xxx.146
www                            A       184.xxx.xxx.146

nginx.conf 使用包括 /etc/nginx/sites-enabled/*; 和 nginx“mydomain.com”配置

server {
    server_name www.mydomain.com;
    rewrite ^(.*) http://mydomain.com$1 permanent;
}
server {
listen 80;
server_name mydomain.com www.mydomain.com;

   # access_log /srv/www/mydomain.com/logs/access.log;
    error_log /srv/www/mydomain.com/logs/error.log;
    root /srv/www/mydomain.com/public_html;
    set $noadmin 1;

    location / {
        try_files $uri $uri/ /index.php?$args;
        index index.html index.htm index.php;
    }

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

     location ~ \.flv$ {
            flv;
            root /srv/www/mydomain.com/public_html;
     }

     location ~ \.mp4$ {
            root /srv/www/mydomain.com/public_html;
            mp4;
     }

     # use fastcgi for all php files
        location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/mydomain.com/public_html$fastcgi_script_name;
        include fastcgi_params;
   }

# deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }
}

我可以访问子域,所以我在这方面的可怕尝试似乎有点奏效,我不知道为什么 www.mydomain.com 无法连接,而 http://mydomain.com 会。我正在阅读/学习更多内容,在我了解更改的作用之前,我不想进行更改。我最终可能会破坏更多的 URL。

【问题讨论】:

    标签: nginx named no-www


    【解决方案1】:

    我的解决方案,它对我有用

    server {
        listen 80;
        server_name yourdomainname.com www.yourdomainname.com;
        return 301 https://$server_name$request_uri;
    }
    

    在该文件中写入之前的代码 --> yourdomainname.conf

    nginx

    【讨论】:

      【解决方案2】:

      您正在 nginx.conf 的前几行重写 www.domain.com。如果我没记错的话,重写和重定向是不同的事情。在第一个 server 块上试试这个;

      server {
          server_name  www.mydomain.com;
          return       301 http://mydomain.com$request_uri;
      }
      

      改变

      server_name mydomain.com www.mydomain.com
      

      server_name mydomain.com
      

      在第二个服务器块中。

      【讨论】:

      • 谢谢 :) 从第二个服务器块中删除 www 对我有用,顶部的重写是任何使用 www 的人。而是发送到 http//,这现在也可以使用,尽管我发现我可以提高效率。谢谢你的帮助! wiki.nginx.org/Pitfalls#Taxing_Rewrites
      • 有时添加更多 server_name 指令可能需要您增加 http{ server_names_hash_bucket_size NN } 其中 NN 是一个数字 >= 32
      猜你喜欢
      • 2017-09-29
      • 2020-07-18
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 2017-01-11
      相关资源
      最近更新 更多