我在一個主機上有多個域,nginx 管理所有這些域。每個域都有自己的 SSL 證書(我使用“webroot”插件從 certbot 獲得)。
我在每個組態檔的末尾都有一個服務器塊,作為“包羅萬象”(從這里和這里),為無效的子域回傳 404。
默認 nginx 組態檔default.conf
:
# ...other config...
include /path/to/domain1.conf;
include /path/to/domain2.conf;
# ...other config...
domain1.conf
:
# redirect http to https
server {
listen 80;
listen [::]:80;
server_name domain1.com www.domain1.com
return 301 https://$host$request_uri;
}
# redirect naked to www
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name domain1.com
include path/to/ssl_config.conf
return 301 https://www.$host$request_uri;
}
# serve subdomain www
server {
listen 443 ssl http2;
listen [::]443 ssl http2;
server_name www.domain1.com
include path/to/ssl_config.conf
location / { proxy_pass http://$app; }
}
# catch-all for invalid subdomains (e.g. foo.domain1.com)
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name: _.domain1.com
include path/to/ssl_config.conf
return 404;
}
domain2.conf
:
# same as above, but uses "domain2.com" instead of "domain1.com"
但這會導致錯誤:
[emerg] xxx.xxx.xxx.xxx:443 的重復默認服務器
如果我洗掉了這些default_server
指令,那么它不會正確路由:請求foo.example1.com
重定向到www.foo.example1.com
,然后到www.www.foo.example1.com
,等等。
一切正常,除了無效的子域邏輯。我該如何解決?
uj5u.com熱心網友回復:
您只需要單個默認服務器塊即可捕獲其他服務器塊中未定義的所有其他內容。您不需要在該塊中公開任何真實證書;出于安全目的,請改用虛擬自簽名證書/密鑰。您根本不需要server_name
在該塊中使用任何東西;此外,這_
根本不是通配符。你可以看到
server_name _;
由于歷史原因,在某些 nginx 配置中不時出現,因為在 nginx 0.7.12 之前,您需要指定一些內容 as server_name
,現在不再需要了。官方檔案提供的更多資訊:
在包羅萬象的服務器示例中,
"_"
可以看到奇怪的名稱:server { listen 80 default_server; server_name _; return 444; }
這個名字沒有什么特別之處,它只是無數與任何真實姓名不相交的無效域名之一。其他無效名稱如
"--"
和"!@#"
同樣可以使用。
一個包羅萬象的塊的示例可以是以下示例:
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 default_server ssl;
listen [::]:443 default_server ssl;
ssl_certificate /some/path/any.crt;
ssl_certificate_key /some/path/any.key;
return 444; # silently drop the connection
# or you can define some landing page here
}
要在一行中生成一對自簽名密鑰/證書,您可以使用以下命令:
openssl req -nodes -new -x509 -subj "/CN=localhost" -keyout /some/path/any.key -out /some/path/any.crt
一個好的做法是將此存根服務器塊作為/etc/nginx/conf.d/default.conf
或/etc/nginx/sites-enabled/default
檔案內容,具體取決于您實際使用的虛擬主機服務風格(可以在 ServerFault 上找到關于此主題的長篇閱讀討論)。
如果您使用的是 certbot,請不要讓它為您自動生成重定向服務器塊,以防使用此類存根服務器塊。由于具有無效請求主機名的所有內容都將由存根塊處理,而不是類似
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
使用以下一個:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
要將非 www 重定向到 www 并將 HTTP 重定向到 HTTPS,請為每個托管的 vhost 使用三個服務器塊:
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
# ssl certificate/key for 'example.com' domain here
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.example.com;
# ssl certificate/key for 'www.example.com' domain here
# the main configuration part
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490067.html
上一篇:從API下載PDF