nginx子域和域重写w代理传递

时间:2021-03-16 11:05:24

I need these two types of rewrites:

我需要这两种重写:

subdomain.domain.com => domain.com/website/subdomain

subdomain.domain.com = > domain.com/website/subdomain

otherdomain.com => domain.com/userdomain/otherdomain.com

otherdomain.com = > domain.com/userdomain/otherdomain.com

My problem is that I want the user to see subdomain.domain.com, and otherdomain.com, not the redirected version. My current rewrite in nginx works, but the user's URL shows the rewrite, and I want this to be transparent to the user, any ideas?:

我的问题是,我希望用户看到subdomain.domain.com和otherdomain.com,而不是重定向版本。我目前在nginx中的重写是可行的,但是用户的URL显示了重写,我希望对用户透明,有什么想法吗?

upstream domain_server { server localhost:8000 fail_timeout=0; }     

server {
        listen  80;
        root  /var/www/domain.com;

        server_name domain.com ~^(?<subdomain>.*)\.domain\.com$ ~^(?<otherdomain>.*)$;
        if ( $subdomain ) {
                rewrite ^ http://domain.com/website/$subdomain break;
        }
        if ( $otherdomain ) {
                rewrite ^ http://domain.com/userdomain/$otherdomain break;
        }

        location / {
                proxy_redirect off;
                proxy_buffering off;
                proxy_set_header Host $http_host;
                proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
                if (!-f $request_filename) {
                        proxy_pass http://domain_server;
                        break;
                }
        }

}

1 个解决方案

#1


24  

With nginx you don't need rewrites at all.

对于nginx,你根本不需要重写。

upstream domain_server { server localhost:8000 fail_timeout=0; }

proxy_set_header Host domain.com;
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;

server {
    listen  80 default_server;

    location / {
        proxy_pass http://domain_server/userdomain/$http_host;
    }
}

server {
    listen  80;
    server_name domain.com;

    root  /var/www/domain.com;

    location / {
        try_files $uri @backend;
    }

    location @backend {
        proxy_pass http://domain_server;
    }
}

server {
    listen  80;
    server_name ~^(?<subdomain>.+)\.domain\.com$;

    location / {
        proxy_pass http://domain_server/website/$subdomain$request_uri;
    }
}

#1


24  

With nginx you don't need rewrites at all.

对于nginx,你根本不需要重写。

upstream domain_server { server localhost:8000 fail_timeout=0; }

proxy_set_header Host domain.com;
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;

server {
    listen  80 default_server;

    location / {
        proxy_pass http://domain_server/userdomain/$http_host;
    }
}

server {
    listen  80;
    server_name domain.com;

    root  /var/www/domain.com;

    location / {
        try_files $uri @backend;
    }

    location @backend {
        proxy_pass http://domain_server;
    }
}

server {
    listen  80;
    server_name ~^(?<subdomain>.+)\.domain\.com$;

    location / {
        proxy_pass http://domain_server/website/$subdomain$request_uri;
    }
}