我的网站 IP 为 1.2.3.4
在我的域名提供商上,onesite.com 和 anothersite.com 都指向 1.2.3.4
使用 Nginx,我配置了两个站点:
server {
listen 1.2.3.4:80;
server_name www.oneserver.com;
rewrite ^(.*) http://onserver.com$1 permanent;
}
server {
listen 1.2.3.4:80;
server_name onserver.com;
location / {
fastcgi_pass 127.0.0.1:8878;
[..]
和:
server {
listen 1.2.3.4:80;
server_name myapp.anotherserver.com;
location / {
fastcgi_pass unix:/tmp/myapp.sock;
[..]
当我访问 myapp.anotherserver.com 时,我被重定向到 oneserver.com
有什么帮助吗?
答案1
答案2
问题是 Nginx 选择默认服务器,并在没有明确定义服务器的情况下为任何请求提供服务。我的解决方法是定义一个不执行任何操作的默认服务器。
# This just prevents Nginx picking a random default server if it doesn't know which
# server block to send a request to
server {
# You can add 443/ssl if you need to
listen 80 default_server;
server_name _;
access_log off; log_not_found off;
# "I'm a teapot", effectively "go away" https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
# Code 403 (forbidden), 410 (gone) or 501 (not implemented) is probably a better choice https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
return 418;
}