【问题标题】:Base URLs like example.com not working in NGinx像 example.com 这样的基本 URL 在 NGinx 中不起作用
【发布时间】:2015-07-22 12:03:38
【问题描述】:

我计划在一台装有 NGinx 和 php5-fpm 的服务器上运行 12 个站点。我在每个 conf 文件中使用一个服务器块来设置它们,所有这些都包含在主 nginx.conf 文件中。它是 Wordpress、PhpMyAdmin 和 PHP 站点的混合体。 wordpress 和 PhpMyAdmin 站点运行良好,但 PHP 站点却不行。意思是,当我打开 example.com 时,Chrome 说连接被拒绝,并且在 NGinx 日志上没有任何传入连接的痕迹。 test.example.com 同时拉起默认站点(因为我当时没有配置 test.example.com)。

我从工作站点复制了 nginx 配置来设置不工作的站点,但没有运气。工作站点和非工作站点之间 nginx 配置的唯一区别是 server_name 指令。

经过2个多小时的反复检查,我发现server_name为pqr.example.com的站点可以正常工作,而example.com的站点不行。所有工作站点都配置为使用子域 URL,这可能就是它们工作的原因。

我的问题是 -
1. 我在配置中缺少什么以使 abc.com 正常工作?
2. 我有两个站点,example.com 和 example.net,我试图在同一台服务器上运行。这对 NGinx 来说会是个问题吗?
3. Nginx 区分 example.com、test.example.com 和 example.net 有问题吗?
4. 我还注意到,如果 www.example.net 有效,www.example.com 无效,反之亦然,这意味着我必须为每个名称为 abc 的站点分配不同的子域,例如 www.example.net 和test.example.com。这是 Nginx 的标准/预期行为,还是我遗漏了什么?
5. 我所有的基本 URL 都从 http://example.com 自动重定向到 http://www.example.com;我如何找出重定向发生的位置?

以下是我遇到问题的 Nginx 配置文件,被截断以包含重要部分;如果需要更多信息,请告诉我。

主要的 nginx.conf 文件 -

user www-data www-data;
pid /var/run/nginx.pid;
worker_processes 4;
worker_rlimit_nofile 100000;

events {
    worker_connections  4096;
    include /etc/nginx.custom.events.d/*.conf;
}

http {
    default_type application/octet-stream;

    access_log off;
    error_log  /var/log/nginx/error.log crit;
    .......
    server_tokens off;

    include proxy.conf;
    include fcgi.conf;

    include conf.d/*.conf;
    include /etc/nginx.custom.d/*.conf;
}

include /etc/nginx.custom.global.d/*.conf;

这是一个有效的博客的完整工作 .conf 文件。所有其他网站都有这个完整的配置,因为它们只是基本的 PHP 网站。

server {
    listen *:80;    

    server_name blog.example.com;

    access_log /var/log/nginx/blog-example.access.log;
    error_log /var/log/nginx/blog-example.error.log;

    root /var/www/example/blog;
    index index.html index.htm index.php;

    # This order might seem weird - this is attempted to match last if rules below fail.
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

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

    # Directives to send expires headers and turn off 404 error logging.
    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
           access_log off; log_not_found off; expires max;
    }

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

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

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    # Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
    location ~ /\. {
        deny all;
    }

    # Deny access to any files with a .php extension in the uploads directory
    # Works in sub-directory installs and also in multisite network
    # Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
    location ~* /(?:uploads|files)/.*\.php$ {
        deny all;
    }

    location ~ [^/]\.php(/|$) {

        # Zero-day exploit defense.
        # http://forum.nginx.org/read.php?2,88845,page=3
        # Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
        # Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine.  And then cross your fingers that you won't get hacked.
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_index index.php;
        include fcgi.conf;
        fastcgi_pass unix:/var/run/php-fcgi-blog-example-php-fcgi-0.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    }

}

这是 example.com 的截断 .conf 文件

server {
    listen *:80;    

    server_name example.com www.example.com test.example.com;

    access_log /var/log/nginx/examplecom.access.log;
    error_log /var/log/nginx/examplecom.error.log;

    root /var/www/example/com;
    index index.html index.htm index.php;

    # This order might seem weird - this is attempted to match last if rules below fail.
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    ........

    location ~ [^/]\.php(/|$) {
        ......
        fastcgi_index index.php;
        include fcgi.conf;
        fastcgi_pass unix:/var/run/php-fcgi-examplecom-php-fcgi-0.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

这是 example.net 的截断文件

server {
    listen *:80;

    server_name example.net www.example.net test.example.net;

    access_log /var/log/nginx/examplenet.access.log;
    error_log /var/log/nginx/examplenet.error.log;

    root /var/www/example/net;
    index index.html index.htm index.php;

    # This order might seem weird - this is attempted to match last if rules below fail.
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    ........

    location ~ [^/]\.php(/|$) {
        ......
        fastcgi_index index.php;
        include fcgi.conf;
        fastcgi_pass unix:/var/run/php-fcgi-examplenet-php-fcgi-0.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

【问题讨论】:

    标签: php nginx


    【解决方案1】:

    意思是,当我打开 example.com 时,Chrome 说连接被拒绝,并且在 NGinx 日志上没有传入连接的痕迹。 test.example.com 同时拉起默认站点(因为我当时没有配置 test.example.com)。

    嗯,您的服务器正在监听。您可能没有正确配置 DNS 记录,或者存在 DNS 缓存。设置你的主机文件来测试这个理论。

    【讨论】:

    • 我在发布之前使用 nslookup 检查了我的 DNS 设置,并且我的 example.com 的所有变体/子域都指向同一个 IP。我不确定请求的去向,但现在问题已解决。在 NGinx 和 php5-fpm 的重新启动次数之间,以及大约 8 小时不理会它,问题自行解决,我现在可以访问所有子域而无需任何配置更改。看起来 DNS 缓存可能是罪魁祸首,因为这解释了为什么 Chrome 说拒绝并且我在 NGinx 上没有看到任何日志。谢谢你的回答!
    猜你喜欢
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2013-10-03
    • 2016-01-11
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多