Ubuntu

Nginx 位置匹配子域和域

  • July 2, 2020

我將只在我的子域上提供 webapp,admininstration.example.com但也可以從 example.com 訪問它。這是我的 nginx 配置:

http {

 server {
   server_name = _;
   listen 80 default_server;
   return 301 https://$host$request_uri;
 }

 server {
   server_name = example.com;
   listen 80;
   location / {
     return 404;
   }
 }

 server {
   listen 443 ssl default_server;
   listen [::]:443 ssl default_server;

   server_name administration.example.com;

   ssl_certificate /etc/nginx/certificate.cer;
   ssl_certificate_key /etc/nginx/ppk2.key;

   ssl_session_cache builtin:1000 shared:SSL:10m;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
   ssl_prefer_server_ciphers on;

   location /pgadmin4/ {
     proxy_set_header X-Script-Name /pgadmin4;
     proxy_set_header X-Scheme $scheme;
     proxy_set_header Host $host;
     proxy_pass http://localhost:5050/;
     proxy_redirect off;
   }
 }
}

知道配置的哪一部分是錯誤的嗎?

您的 HTTPS 在埠 443 上只有一個server{}

location / { return 404; }僅適用於埠 80 上的純 HTTP 。你可以例如

server {
   listen 80;
   listen 443 ssl;
   server_name example.com;

   ssl_certificate /etc/nginx/certificate.cer;
   ssl_certificate_key /etc/nginx/ppk2.key;

   location / {
       return 404;
   }
}

順便說一句,=語法中沒有server_name.

引用自:https://serverfault.com/questions/1023745