NGINX proxy_pass将子域添加为request_uri参数

时间:2020-10-11 22:46:47

标签: nginx nginx-reverse-proxy nginx-location nginx-config

我目前已为mywebsite.com * .mywebsite.com(通配符)配置了LetsEncrypt,它可以通过nginx配置将处理非http和非www访问重定向到https。

我还有一个用于动态子域的用例-我将从我的应用程序渲染一条特定路径。

示例

company1.mywebsite.com --> https://mywebsite.com/organization/company1
company1.mywebsite.com/posts --> https://mywebsite.com/organization/company1/posts

company2.mywebsite.com --> https://mywebsite.com/organization/company2
company2.mywebsite.com/posts --> https://mywebsite.com/organization/company2/posts

and so on.. (Browser address bar does not change, but the app gets rendered from that proxy pass URL as configured)

带有/posts的URL可以正常工作,但是当我访问没有request_uri的URL(例如company1.mywebsite.com)时,它们会在浏览器中自动重定向到company1.mywebsite.com/organization/company1,这是不正确的。

基于我的配置,它不应重定向并停留在company1.mywebsite.com

这是我的Nginx配置

server {
    #root /var/www/mywebsite/html;
    #index index.html index.htm index.nginx-debian.html;

    server_name mywebsite.com www.mywebsite.com;
    
    location /_next/ {
        alias /root/test-project/.next/;
        expires 30d;
        access_log on;
    }

    location / {
        proxy_pass http://localhost:8000;
        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;
        #try_files $uri $uri/ =404;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.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 {
    server_name ~^(?<subdomain>[^.]+).mywebsite.com;

    location /_next/ {
        alias /root/test-project/.next/;
        expires 30d;
        access_log on;
    }

    location / {
        proxy_pass http://localhost:8000/organization/$subdomain$request_uri;
        proxy_set_header Host $host;
        #return 301 $scheme://mywebsite.com/organization/$subdomain$request_uri;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.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.mywebsite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host ~ ^(?<subdomain>[^.]+).mywebsite.com$) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = mywebsite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;

    server_name mywebsite.com www.mywebsite.com *.mywebsite.com;
    return 404; # managed by Certbot
}

我不确定是什么原因导致那些没有request_uri路径的URL的额外重定向。

0 个答案:

没有答案
相关问题