
我的網站 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;
}