douyiyi5284
douyiyi5284
2015-05-11 20:19
浏览 67
已采纳

像example.com这样的基本URL无法在NGinx中运行

I have 12 sites that I plan to run on a single server that has NGinx and php5-fpm on it. I set them all up using one server block per conf file, all included by the main nginx.conf file. It's a mix of Wordpress, PhpMyAdmin, and PHP sites. The wordpress and PhpMyAdmin sites are working fine, but the PHP sites are not. Meaning, when I pull up example.com, Chrome says connection refused, and there's no trace of an incoming connection on NGinx logs. test.example.com pulls up the default site(because I didn't configure test.example.com then) at the same time.

I copied the nginx configs from the working sites to set up the sites that are not working, but no luck. The only difference in nginx config between the working and non-working sites are the server_name directive.

After checking and rechecking for over 2 hours, I found out that the sites that have the server_name as pqr.example.com work, but the ones with example.com don't. All of the working sites are configured to use subdomain URLs, and that's probably why they're working.

My questions are -
1. What am I missing in the config to make the abc.com work ?
2. I have two sites, example.com and example.net that I'm trying to run on the same server. Is that going to be a problem for NGinx ?
3. Does Nginx have a problem with differentiating between example.com, test.example.com, and example.net ?
4. I also noticed that if www.example.net works, www.example.com doesn't and vice versa, which means I have to assign each site that has the name abc in it different subdomains like www.example.net and test.example.com. Is this a standard/expected behavior of Nginx, or am I missing something ?
5. All of my base URLs auto redirect from http://example.com to http://www.example.com; How do I find out where that redirect is happening ?

Below are the Nginx config files that I'm having problems with, truncated to include the important parts; Please let me know if more info is needed.

Main nginx.conf file -

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;

Here is the full working .conf file for a blog that works. All other sites have this full config, since they are just basic PHP sites.

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;

    }

}

Here's the truncated .conf file for example.com

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;
    }

}

Here's the truncated file for 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;
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqiao1413
    douqiao1413 2015-05-11 20:40
    已采纳

    Meaning, when I pull up example.com, Chrome says connection refused, and there's no trace of an incoming connection on NGinx logs. test.example.com pulls up the default site(because I didn't configure test.example.com then) at the same time.

    Well, your server is listening. Chances are you haven't configured your DNS records correctly, or there is DNS caching. Set your host file to test this theory.

    点赞 评论

相关推荐