我需要找出如何只监听一个端口-主机名组合并在所有其他请求上返回 404。
我的例子:
我已经为 owncloud 设置了一个子域,它使用 SSL 并监听端口 12345 而不是 443。我们假设这个子域是 oc.example.com。所以我希望 nginx 只听 https://oc.example.com:12345但不是为了
- http://oc.example.com:12345
- > https://oc.example.com :X 其中 X != 12345
- https://BLABLA.example.com:12345
- http://IP.IP.IP.IP :12345
- https://IP.IP.IP.IP:12345
- 等等
如果请求任何不完全匹配 https://oc.example.com:12345 的资源应该返回一个错误(例如 404 not found)或者服务器根本不应该响应。
到目前为止我的配置如下:
server {
# I think there's something wrong here?
listen 12345;
server_name oc.example.com;
ssl on;
ssl_certificate /etc/ssl/nginx/owncloud/owncloud.crt;
ssl_certificate_key /etc/ssl/nginx/owncloud/owncloud.key;
add_header Strict-Transport-Security max-age=31536000;
add_header X-Frame-Options DENY;
# Path to the root of your installation
root /var/some/path/to/my/owncloud/;
client_max_body_size 10G; # set max upload size
fastcgi_buffers 64 4K;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
index index.php;
error_page 403 = /core/templates/403.php;
error_page 404 = /core/templates/404.php;
... here are the definitions of my locations
}
看了文档发现nginx首先是通过端口号来寻找正确的服务器定义。如果有多个匹配项,则使用 server_name 来查找正确的定义。但我只有一个定义!
有人知道如何解决这个问题吗?
最佳答案
nginx 检查主机头并选择合适的虚拟主机..为了让它放弃所有其他东西,只需使用 default_server 指令添加一个“包罗万象”:
server {
listen 80 default_server;
return 404;
}
除了 12345 之外,所有对 oc.example.com:X 的请求都将被默认丢弃,因为您没有定义监听其他端口的其他虚拟主机。
关于ssl - 只听一个子域和一个端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549221/