![](/img/trans.png)
[英]Nginx rewriteurl from example.com/script.php?param=value to example.com/value
[英]Base URLs like example.com not working in NGinx
我有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指令。
經過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;
}
}
意思是,當我打開example.com時,Chrome瀏覽器顯示連接被拒絕,並且在NGinx日志中沒有任何傳入連接的痕跡。 test.example.com同時拉出默認站點(因為我當時沒有配置test.example.com)。
好吧,您的服務器正在監聽。 您可能沒有正確配置DNS記錄,或者存在DNS緩存。 設置主機文件以測試該理論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.