【发布时间】:2022-11-28 21:58:19
【问题描述】:
我习惯用 Apache 来做这件事,并且在用 NGINX 复制我想要的东西时遇到了问题,因为我是后者的新手。据我了解,Server Blocks 或多或少相当于 Apache 的 VirtualHosts 的 NGINX
我想要的是:
映射/服务相同的应用程序:
- api.domain1.com
- api.domain2.com
我有的:
在 Ubuntu 20.04 上运行的 Express 应用程序和 NGINX
-
api.domain1.com- 指向我IP的记录 -
api.domain1.com- 服务我的应用程序(工作正常) -
api.domain2.com- CNAME 记录指向api.domain1.com -
api.domain2.com- 提供默认的Welcome to nginx!页面
我的问题)
- 我是否必须为映射到我的应用程序的每个域创建一个“服务器块”?
- 我可以用一个 A) 服务器块文件处理多个(子)域,还是 B)每个(子)域我需要一个服务器块
在我的服务器块文件中,我有:
~/etc/nginx/sites-available/my_apiserver { root /var/www/my_api/html; index index.html index.htm index.nginx-debian.html; server_name api.domain1.com www.api.domain1.com; location / { # try_files $uri $uri/ =404; proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } listen [::]:443 ssl ipv6only=on; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/api.domain1.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/api.domain1.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = www.api.domain1.com) { return 301 https://$host$request_uri; } # managed by Certbot if ($host = api.domain1.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; listen [::]:80; server_name api.domain1.com www.api.domain1.com; return 404; # managed by Certbot }
【问题讨论】: