![](/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.