nginx:设置不同服务器的特定路径

fbe*_*nce 2 nginx

我有一个来自我的大学的域名,我在那里运行着一项服务:

    server {
    listen              443 default_server ssl;
    server_name         example.uni.com;
    keepalive_timeout   70;

    ssl_certificate     xxx.crt;
    ssl_certificate_key xxx.key;


    location / {
        proxy_pass http://localhost:8081;
        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;
    }


}
Run Code Online (Sandbox Code Playgroud)

我想要实现的目标是指向example.uni.com/specificaddress在不同本地主机端口上运行的不同服务,而不必修改在 8081 上运行的服务(更好的是specificaddress.example.uni.com,但我相信我自己无法做到这一点)。这怎么可能呢?不幸的是,简单地创建另一个服务器并server_name设置为example.uni.com/specificaddress不起作用(这不是一个大惊喜,它是由运行在 8081 上的服务处理的)。

nu1*_*73R 5

您可以使用代理通过不同的端口添加新的位置块。

例子

location / {
        proxy_pass http://localhost:8081;
        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;
}

location /specificaddress {
        proxy_pass http://localhost:8082;
        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;
}
Run Code Online (Sandbox Code Playgroud)