首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >像example.com这样的基本URL不能在NGinx中工作

像example.com这样的基本URL不能在NGinx中工作
EN

Stack Overflow用户
提问于 2015-05-11 20:19:44
回答 1查看 1.2K关注 0票数 0

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

我从工作网站上复制了nginx的信息,建立了那些不起作用的网站,但没有运气。nginx配置在工作站点和非工作站点之间唯一的区别是server_name指令。

经过两个多小时的检查和重新检查,我发现使用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文件-

代码语言:javascript
复制
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站点。

代码语言:javascript
复制
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;

    }

}

下面是截断的.conf文件,用于example.com

代码语言:javascript
复制
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的截断文件

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-11 20:40:06

这意味着,当我拔出example.com时,Chrome表示连接被拒绝,并且在NGinx日志中没有任何传入连接的痕迹。test.example.com同时提取默认站点(因为我当时没有配置test.example.com )。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30176904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档