我在Nginx上设置了2个网站,正确的应该根据访问的域加载.
例如:
website1.com
website2.com
但是,当访问这些域中的任何一个时,它们都会加载website1.com.
website1.com配置
server {
listen 80;
listen [::]:80 ipv6only=on;
root /var/www/website1.com;
index index.PHP index.html index.htm;
# Make site accessible from http://localhost/
server_name website1.com;
location / {
# First attempt to serve request as file,then
# as directory,then fall back to displaying a 404.
try_files $uri $uri/ /index.PHP?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/Nginx/naxsi.rules
}
location ~ \.PHP${
try_files $uri /index.PHP =404;
fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
fastcgi_pass unix:/var/run/PHP5-fpm.sock;
fastcgi_index index.PHP;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Only for Nginx-naxsi used with Nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/Nginx/html;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.PHP${
# fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in PHP.ini
#
# # With PHP5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With PHP5-fpm:
# fastcgi_pass unix:/var/run/PHP5-fpm.sock;
# fastcgi_index index.PHP;
# include fastcgi_params;
#}
# deny access to .htaccess files,if Apache's document root
# concurs with Nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
website2.com配置
server {
listen 80;
listen [::]:80 ipv6only=on;
root /var/www/website2.com;
index index.PHP index.html index.htm;
# Make site accessible from http://localhost/
server_name website2.com;
location / {
# First attempt to serve request as file,then fall back to displaying a 404.
try_files $uri $uri/ /index.PHP?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/Nginx/naxsi.rules
}
location ~ \.PHP${
try_files $uri /index.PHP =404;
fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
fastcgi_pass unix:/var/run/PHP5-fpm.sock;
fastcgi_index index.PHP;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Only for Nginx-naxsi used with Nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/Nginx/html;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.PHP${
# fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in PHP.ini
#
# # With PHP5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With PHP5-fpm:
# fastcgi_pass unix:/var/run/PHP5-fpm.sock;
# fastcgi_index index.PHP;
# include fastcgi_params;
#}
# deny access to .htaccess files,if Apache's document root
# concurs with Nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Nginx.conf
user www-data;
worker_processes 4;
pid /run/Nginx.pid;
events {
worker_connections 4096;
multi_accept on;
}
http {
##
# Basic Settings
##
#sendfile on;
#tcp_nopush on;
#tcp_nodelay on;
#keepalive_timeout 10;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/Nginx/mime.types;
default_type application/octet-stream;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
##
# Logging Settings
##
access_log off;
log_not_found off;
error_log /var/log/Nginx-error.log warn;
#access_log /var/log/Nginx/access.log;
#error_log /var/log/Nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+RSS text/javascript;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+RSS text/javascript;
##
# Nginx-naxsi config
##
# Uncomment it if you installed Nginx-naxsi
##
#include /etc/Nginx/naxsi_core.rules;
##
# Nginx-passenger config
##
# Uncomment it if you installed Nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/Nginx/conf.d/*.conf;
include /etc/Nginx/sites-enabled/*;
open_file_cache max=2000 inactive=20s;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors off;
client_max_body_size 50M;
client_body_buffer_size 1m;
client_body_timeout 15;
client_header_timeout 15;
keepalive_timeout 2 2;
send_timeout 15;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_connect_timeout 3s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
reset_timedout_connection on;
server_names_hash_bucket_size 100;
}
#mail {
# # See sample authentication script at:
# # http://wiki.Nginx.org/ImapAuthenticateWithApachePHPScript
#
# # auth_http localhost/auth.PHP;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
最佳答案
如果没有默认站点配置,那么Nginx将在显示未经过专门配置的请求时自动选择第一个可用的配置文件.
原文链接:https://www.f2er.com/nginx/435488.html在这种情况下,当您在任一域之前添加www时,Nginx正在选择website1.com配置
要解决此特定问题,请替换以下配置行
website1.com配置
server_name website1.com www.website1.com;
website2.com配置
server_name website2.com www.website2.com;
您也可以使用* .website1.com或* .website2.com来捕获所有请求.
你应该有一个默认的网站配置,因为有一些像这样的怪癖,可以避免一个.即使它只是返回400错误.