使用NGINX反向代理的CNAMEMap子域

wkyowqbh  于 2022-12-03  发布在  Nginx
关注(0)|答案(1)|浏览(449)

我已经习惯了用Apache来做这些事情,而且我在用NGINX复制我想要的东西时遇到了问题,因为我对后者是新手。

我想要的:

Map/服务同一应用程序:

  • api.domain1.com
  • api.domain2.com
    我拥有的:

运行在Ubuntu 20.04上的Express应用程序& NGINX

  • api.domain1.com-指向我的IP的记录
  • api.domain1.com-为我的应用提供服务(运行良好)
  • api.domain2.com-指向api.domain1.com的CNAME记录
  • api.domain2.com-提供默认的Welcome to nginx!页面

我的问题
1.我是否必须为Map到我的应用程序的每个域创建一个“服务器块”?
1.我可以用一个服务器块文件处理多个(子)域吗A)还是B)每个(子)域都需要一个服务器块
在我的服务器块文件中,我有:
~/etc/nginx/sites-available/my_api

server {

        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



}
7gs2gvoe

7gs2gvoe1#

好吧,在我尝试了下面的方法后不久(为了举例的目的简化了一点)&它似乎起到了作用。具体来说:向同一文件添加额外的server{}指令似乎允许通过多个(子)域为同一应用提供服务。

server {

        root /var/www/iris_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;
        }

}

server {

        root /var/www/iris_api/html;
        index index.html index.htm index.nginx-debian.html;

        server_name api.domain2.com www.api.domain2.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;
        }

}

相关问题