nginx子域和域重写w代理通过 [英] nginx subdomain and domain rewrite w proxy pass
本文介绍了nginx子域和域重写w代理通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要这两种类型的重写:
I need these two types of rewrites:
subdomain.domain.com => domain.com/website/subdomain
otherdomain.com => domain.com/userdomain/otherdomain.com
我的问题是我希望用户看到 subdomain.domain .com
和 otherdomain.com
,而不是重定向的版本。我目前在nginx中的重写工作,但用户的URL显示重写,我希望这对用户透明,任何想法?:
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?:
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;
}
}
}
推荐答案
使用nginx,您根本不需要重写。
With nginx you don't need rewrites at all.
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;
}
}
- http://nginx.org/r/proxy_pass
- http://wiki.nginx.org/IfIsEvil
- http://wiki.nginx.org/Pitfalls
- http://nginx.org/en/docs/http/converting_rewrite_rules.html
- http://nginx.org/r/proxy_pass
- http://wiki.nginx.org/IfIsEvil
- http://wiki.nginx.org/Pitfalls
- http://nginx.org/en/docs/http/converting_rewrite_rules.html
这篇关于nginx子域和域重写w代理通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文