【问题标题】:Nginx virtual host loading wrong siteNginx 虚拟主机加载错误的站点
【发布时间】:2017-05-05 07:21:20
【问题描述】:

我有三个域的 Nginx。

这些网站按字母顺序是 d.com、g.com 和 m.com。

所有三个站点都是单站点 WordPress 安装。

g.com 是带有letsencrypt 证书的https,并按预期加载。

m.com 不是 https 并按预期加载

d.com 加载 m.com 而不是它自己的文件夹。

我已经搜索和阅读了两个星期,但我无法弄清楚我做错了什么。

新信息

我发现它的 DNS 仍在我的旧主机 (mt) 上,它正确指向这里,但我决定将它移到新主机 (linode)。

现在,如果我使用 www.d.com,我会从正确的文件夹加载正确的站点。但是,如果我不使用 www 而只使用 d.com,我会被重定向到 www.m.com 作为完整重定向。

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        client_max_body_size 150M;

        # server_tokens off;
        server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        #fastcgi
        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        # /snippits/ssl-params.conf
        ##

        #ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        #ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        # Logs set in server blocks
        ##
        error_log /var/log/nginx/http_error.log error;

        ##
        # Gzip Settings
        # /conf.d/gzip.conf
        ##

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

/etc/nginx/sites-available/d.com

server {
        listen 80;
        listen [::]:80;

        server_name d.com www.d.com;

        include snippets/expires-headers.conf;

        root /var/www/html/d.com/public_html;

        index index.php;

        access_log /var/log/nginx/d.com/www-access.log;
        error_log /var/log/nginx/d.com/www-error.log error;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_intercept_errors on;
                include fastcgi_params;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_param SCRIPT_FILENAME /var/www/html/d.com/public_html$fastcgi_script_name;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

/etc/nginx/sites-available/g.com

server {
        listen 80;
        listen [::]:80;

        server_name g.com www.g.com;

    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    ssl_certificate /var/lib/acme/live/g.com/fullchain;
    ssl_certificate_key /var/lib/acme/live/g.com/privkey;

    include snippets/ssl-params.conf;
    include snippets/expires-headers.conf;

    server_name g.com www.g.com;

        root /var/www/html/g.com/public_html;
        index index.html index.php;

    access_log /var/log/nginx/g.com/www-access.log;
    error_log /var/log/nginx/g.com/www-error.log error;

    location /.well-known/acme-challenge/ {
        alias /var/run/acme/acme-challenge/;
    }

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                include fastcgi_params;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_param SCRIPT_FILENAME /var/www/html/g.com/public_html$fastcgi_script_name;
        }
}

/etc/nginx/sites-available/m.com

server {
    listen 80;
    listen [::]:80;

    server_name m.com www.m.com;

    include snippets/expires-headers.conf;

    root /var/www/html/m.com/public_html;

    index index.php;

    access_log /var/log/nginx/m.com/www-access.log;
    error_log /var/log/nginx/m.com/www-error.log error;

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

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

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_intercept_errors on;
            include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_param SCRIPT_FILENAME /var/www/html/m.com/public_html$fastcgi_script_name;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }
}

【问题讨论】:

  • 您是否启用了符号链接或复制文件?
  • 是的,这三个都是符号链接的。

标签: wordpress nginx virtualhost vhosts


【解决方案1】:

您的配置似乎没有任何问题,但我怀疑您通过从 m.com 复制/粘贴配置为 d.com 创建了配置,并在进行任何更改之前保存了文件,然后 NGINX 重新启动或在您对 d.com 配置进行修改之前重新加载其配置,这基本上意味着 NGINX 没有加载您当前的配置。

您可以重新加载配置。在 Ubuntu/Debian 上是这样的:

sudo service nginx reload

您还应该重新检查并绝对确定 /var/www/html/d.com/public_html 和 /var/www/html/m.com/public_html 确实在提供不同的内容。

【讨论】:

  • 我确实以这种方式创建了它,但方向相反。另外,从这开始我已经重启了 Nginx 很多次了。
  • 希望 @num8er 的建议,我有两个建议: - /var/www/html/d.com/public_html 是指向 /var/www/html/m.com/public_html 的符号链接还是反之亦然?
  • 编辑:跳跃* |||我在添加第二个建议之前按了 Enter:您确定 /var/www/html/d.com/public_html 和 /var/www/html/m.com/public_html 提供不同的内容吗?
  • 克里斯蒂亚诺,请提出建议 2 并回答,以便我接受。我仔细检查了一下,我在 wp-config.php 中加载了错误的数据库。
  • 从头开始。我刚刚更改了 Wp-Config.php 中的数据库,它仍在从 m.com 加载内容。实际上......它正在将地址更改为m.com。我输入 d.com,m.com 实际上是在地址中加载了 m.com。好的。现在我想知道我是否有 DNS 问题。
【解决方案2】:

将 DNS 区域文件从 Media Temple 移动到 Linode 后,http://www.d.com 开始从正确的文件夹加载 WordPress 安装,尽管 http://d.com 没有。

我尝试了另一个浏览器,发现在另一个浏览器中现在都可以工作了。

我不明白为什么在这里移动 DNS 区域文件。

【讨论】:

  • 对,您可能已经在常用浏览器上缓存了所有内容,因此在几乎不使用的不同浏览器上,您会立即获得 DNS 更改。
  • 我唯一遇到的问题是它正在更改地址栏中的地址。我从未见过缓存这样做,只有重定向。所以也许我缓存了一个重定向,但我仍然不知道重定向来自哪里。
猜你喜欢
  • 2013-01-06
  • 2016-05-12
  • 2014-07-07
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 2018-07-04
  • 1970-01-01
  • 2016-07-08
相关资源
最近更新 更多