Nginx 总是退回到错误的服务器块
Nginx always falls back on wrong server block
我在可用站点中有两个不同的域和两个 nginx 文件。我还添加了符号链接。
第一个站点配置,应该处理基于 Silex 的 API:
server {
listen 443 ssl;
listen [::]:443 ipv6only=on ssl;
server_name bwr.mydomain1.com;
root /srv/www/bwr/src;
location / {
# try to serve file directly, fallback to front controller
try_files $uri /index.php$is_args$args;
}
# If you have 2 front controllers for dev|prod use the following line instead
# location ~ ^/(index|index_dev)\.php(/|$) {
location ~ ^/index\.php(/|$) {
# the ubuntu default
fastcgi_pass unix:/var/run/php5-fpm.sock;
# for running on centos
#fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/index.php/some-path
# Enable the internal directive to disable URIs like this
# internal;
}
#return 404 for all php files as we do have a front controller
location ~ \.php$ {
return 404;
}
# SSL configuration with letsencrypt
ssl_certificate "/etc/letsencrypt/live/bwr.mydomain1.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/bwr.mydomain1.com/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
ssl_prefer_server_ciphers on;
error_log /var/log/nginx/bwr_error.log;
access_log /var/log/nginx/bwr_access.log;
}
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name bwr.mydomain1.com;
return 301 https://$host$request_uri;
}
第二个域名只服务于一个普通的php网站:
server {
listen 443 ssl default_server;
server_name domain2.ch www.domain2.ch;
root /srv/www/hgtconnect;
index index.html index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
error_log /var/log/nginx/domain2_error.log;
access_log /var/log/nginx/domain2_access.log;
# SSL configuration with letsencrypt
ssl_certificate "/etc/letsencrypt/live/domain2.ch/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/domain2.ch/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
ssl_prefer_server_ciphers on;
}
server {
listen 80 default_server;
server_name domain2.ch www.domain2.ch;
return 301 https://$host$request_uri;
}
每当我打开 https://domain2.ch 时,我都会得到 bwr.domain1.com 的内容。还使用了错误的 ssl 证书,该站点不受信任。
谢谢!
找到灵魂。
两个服务器块上都没有启用 Ipv6。我总是在 Ipv6 网络中请求页面,所以 nginx 总是回落到错误的服务器上,因为那里配置了 ipv6。
我在可用站点中有两个不同的域和两个 nginx 文件。我还添加了符号链接。
第一个站点配置,应该处理基于 Silex 的 API:
server {
listen 443 ssl;
listen [::]:443 ipv6only=on ssl;
server_name bwr.mydomain1.com;
root /srv/www/bwr/src;
location / {
# try to serve file directly, fallback to front controller
try_files $uri /index.php$is_args$args;
}
# If you have 2 front controllers for dev|prod use the following line instead
# location ~ ^/(index|index_dev)\.php(/|$) {
location ~ ^/index\.php(/|$) {
# the ubuntu default
fastcgi_pass unix:/var/run/php5-fpm.sock;
# for running on centos
#fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/index.php/some-path
# Enable the internal directive to disable URIs like this
# internal;
}
#return 404 for all php files as we do have a front controller
location ~ \.php$ {
return 404;
}
# SSL configuration with letsencrypt
ssl_certificate "/etc/letsencrypt/live/bwr.mydomain1.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/bwr.mydomain1.com/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
ssl_prefer_server_ciphers on;
error_log /var/log/nginx/bwr_error.log;
access_log /var/log/nginx/bwr_access.log;
}
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name bwr.mydomain1.com;
return 301 https://$host$request_uri;
}
第二个域名只服务于一个普通的php网站:
server {
listen 443 ssl default_server;
server_name domain2.ch www.domain2.ch;
root /srv/www/hgtconnect;
index index.html index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
error_log /var/log/nginx/domain2_error.log;
access_log /var/log/nginx/domain2_access.log;
# SSL configuration with letsencrypt
ssl_certificate "/etc/letsencrypt/live/domain2.ch/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/domain2.ch/privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
ssl_prefer_server_ciphers on;
}
server {
listen 80 default_server;
server_name domain2.ch www.domain2.ch;
return 301 https://$host$request_uri;
}
每当我打开 https://domain2.ch 时,我都会得到 bwr.domain1.com 的内容。还使用了错误的 ssl 证书,该站点不受信任。
谢谢!
找到灵魂。 两个服务器块上都没有启用 Ipv6。我总是在 Ipv6 网络中请求页面,所以 nginx 总是回落到错误的服务器上,因为那里配置了 ipv6。