NGINX 多服务器 - 第二台服务器始终转到第一台服务器

NGINX 多服务器 - 第二台服务器始终转到第一台服务器

我刚刚安装了带有 php5-fpm 的 nginx,并且通过单个 vhost 服务器一切都运行良好。

我刚刚为 wordpress 安装添加了辅助服务器(不同的域名),当我访问它时,它总是重定向到一直在工作的第一个虚拟主机。奇怪的是,在错误日志中,我可以看到它无法从 wp-content 加载内容,但它试图从第一个域中提取内容...

第一个域名且始终有效:

server {
        listen         80;
        server_name    domain1.com;
        rewrite        ^ https://$server_name$request_uri? permanent;
}

server {
    # Change these settings to match your machine
    listen 443;
    server_name domain1.com;

    # Everything below here doesn't need to be changed
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    root /data/www/domain1.com/www/;
    index index.html index.htm index.php;

    ssl on;
    ssl_certificate /data/ssl/domain1.pem;
    ssl_certificate_key /data/ssl/domain1.key;

    ssl_session_timeout 5m;

    if ($host ~* ^www\.(.*))
    {
            set $host_without_www $1;
            rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

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

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

    location ~* \.(?:ico|css|js|gif|inc|txt|gz|xml|png|jpe?g) {
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    location / { try_files $uri $uri/ @rewrites; }

    location @rewrites {
            rewrite ^/([^/\.]+)/([^/]+)/([^/]+)/? /index.php?page=$1&id=$2&subpage=$3 last;
            rewrite ^/([^/\.]+)/([^/]+)/?$ /index.php?page=$1&id=$2 last;
            rewrite ^/([^/\.]+)/?$ /index.php?page=$1 last;
    }

    location /admin { }
    location /install { }

    location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;

            # The next two lines should go in your fastcgi_params
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

第二个域重定向到第一个域但资源正在尝试加载:

server {
    listen         80;
    server_name    domain2.com;

    # Everything below here doesn't need to be changed
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    root /data/www/domain2.com/public_html/;
    index index.html index.htm index.php;

    if ($host ~* ^www\.(.*))
    {
            set $host_without_www $1;
            rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

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

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

    location / {
            # This is cool because no php is touched for static content
            try_files $uri $uri/ /index.php;
    }

    location ~* \.(?:ico|css|js|gif|inc|txt|gz|xml|png|jpe?g) {
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;

            # The next two lines should go in your fastcgi_params
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

错误日志显示尝试提取正确的资源但来自错误的域:

2012/03/11 18:48:45 [error] 29093#0: *26 open() "/data/www/domain1.com/www/wp-content/themes/minimalist/styles/grey.css" failed (2: No such file or directory), client: 60.225.81.244, server: domain1.com, request: "GET /wp-content/themes/minimalist$

答案1

这是因为第一台服务器是默认服务器,它接受任何连接。

我看到您尝试www.domain2.com使用 if 语句进行设置。 www.domain2.com被视为不同的服务器..您可以简单地启动另一个服务器:

server {
...
server_name www.domain2.com;
redirect ^ $scheme://domain2.com$request_uri permanent;
}

这应该够了吧。

相关内容