我有12個站點,我打算在一臺具有NGinx和php5-fpm的服務器上運行。我將它們全部設置爲每個conf文件使用一個服務器塊,全部由主nginx.conf文件包含。它是Wordpress,PhpMyAdmin和PHP站點的組合。 wordpress和PhpMyAdmin網站工作正常,但PHP網站沒有。意思是,當我拉起example.com時,Chrome說連接被拒絕,並且NGinx日誌中沒有傳入連接的蹤跡。 test.example.com會同時拉起默認站點(因爲我沒有配置test.example.com)。像example.com這樣的基本URL在NGinx中不起作用
我複製了工作網站的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;
}
}
我在發佈之前用nslookup檢查了我的DNS設置,並且我的example.com的所有變體/子域都指向相同的IP。我不確定請求的去向,但問題現在已經解決。 在重啓NGinx和php5-fpm的次數以及大約8小時的獨立之間,這個問題已經解決了,我現在可以在沒有任何配置更改的情況下訪問所有子域。 它看起來像DNS緩存可能是罪魁禍首,因爲這解釋了爲什麼Chrome說拒絕,我沒有看到任何關於NGinx的日誌。謝謝你的回答! – radhashankark