域名中的www不在nginx中工作

DOA*_*DOA 4 nginx named no-www

我是新手使用nginx,嗯,新的使用任何不是cpanel的东西......当你包含www时,我在使用nginx工作时遇到问题.在网址中.

www.mydomain.com > not work 404
mydomain.com > works
Run Code Online (Sandbox Code Playgroud)

我不确定我是否在命名配置文件或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 
Run Code Online (Sandbox Code Playgroud)

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; };
};
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

nginx.conf使用include/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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以访问子域名,所以我对此的可怕尝试似乎有点工作,我坚持为什么www.mydomain.com不会连接,而http://mydomain.com会.随着我的进展,我正在阅读/学习更多知识,在我了解变化的内容之前,我不想做出任何改变.我最终可能会破坏URL.

edi*_*igu 6

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

server {
    server_name  www.mydomain.com;
    return       301 http://mydomain.com$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

并改变

server_name mydomain.com www.mydomain.com
Run Code Online (Sandbox Code Playgroud)

server_name mydomain.com
Run Code Online (Sandbox Code Playgroud)

在第二个服务器块中.