14

我是使用 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 

命名.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。

4

2 回答 2

12

You are rewriting www.domain.com on first few lines of nginx.conf. If i'm not wrong, rewriting and redirecting are different things. Try this on first server block;

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

and change

server_name mydomain.com www.mydomain.com

to

server_name mydomain.com

in second server block.

于 2012-04-01T02:50:51.810 回答
1

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

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

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

nginx

于 2019-10-15T01:46:17.423 回答