如何使用Nginx将子域指向特定端口

时间:2018-12-26 12:22:22

标签: amazon-web-services nginx portforwarding

我试图将子域指向服务器的特定端口,但是问题是当我转到该子域时,它显示: 无法访问此网站

我需要的是这样的: 当我转到https://auth.domain.com时,将我指向aws上的服务器:https://ec2.domain.aws:8443,但操作失败

这是我的nginx.conf文件:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen 80;
        server_name  localhost;

        root   /angularapp/dist;
        index  index.html index.htm;
        include /etc/nginx/mime.types;

        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
            try_files $uri $uri/ /index.html;
        }
    }
    server {
        listen 80;
        server_name  auth.domain.com;

        location / {
            proxy_pass https://ec2.domain.aws:8443;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

我要在这里做的是,在第一台服务器上访问本地主机(domain.com)以仅显示index.html文件,该文件运行正常,而在第二台服务器上,用户尝试访问auth.domain.com使其指向https://ec2.domain.aws:8443时的服务器。

我错过了什么吗?还是这不是我应该做的方式?

我在第二个服务器解决方案上也找到了它: https://www.digitalocean.com/community/questions/how-do-i-point-my-custom-domain-to-my-ip-port-41-111-20-36-8080

谢谢

2 个答案:

答案 0 :(得分:0)

请尝试使用以下配置。

location auth.domain.com {
               proxy_pass https://ec2.domain.aws:8443;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header X-Forwarded-Proto $scheme;
        }

答案 1 :(得分:0)

将HTTP Host标头设置为等于您的AWS域名:

proxy_set_header Host ec2.domain.aws;

如果这没有帮助,请尝试使用proxy_ssl_server_name指令为代理的HTTPS请求打开SNI:

proxy_ssl_server_name on;