简体   繁体   English

使子目录显示nginx中子域的内容

[英]make a subdirectory show the content of a subdomain in nginx

I want to handle this: 我想处理:

blog.example.com => example.com/blog

blog.example.com/xxx => example.com/blog/xxx

in both samples there is nothing in blog subdirectory, and the code which should handle the blog is in subdomain. 在这两个示例中,博客子目录中都没有任何内容,应该处理博客的代码位于子域中。 and just i want to show the url as showed above. 而我只想显示上面显示的网址。

so. 所以。 i want to forward(redirect without changing url) a subdirectory to subdomain. 我想将子目录转发(重定向而不更改url)到子域。

is there any nginx configuration to do that? 是否有任何nginx配置可以做到这一点?

You could have the following in your NGINX configuration. 您的NGINX配置中可能包含以下内容。

server {
    listen 80;
    server_name blog.example.com;

    location / {
        return 301 $scheme://example.com/blog$request_uri;
    }
}

server {
    listen 80;
    server_name example.com;

    location /blog/ {
        <your code goes here>
    }
}

This takes any incoming requests to blog.example.com and redirects to example.com/blog along with the requested URI eg. 这会将所有传入请求都发送到blog.example.com并重定向到example.com/blog以及所请求的URI,例如。 blog.example.com/latest would redirect to example.com/blog/latest blog.example.com/latest将重定向到example.com/blog/latest

location = /blog {
    return 302 /blog/;
}
location /blog/ {
    proxy_pass http://blog.example.com/;
}

Note that the / in proxy_pass is very important (without it the /blog part won't be stripped out in the request to upstream). 请注意, proxy_pass中的/非常重要(没有它, /blog部分将不会在上游请求中被剥离)。

Some further details on the rationale of two independent location statements are available at https://serverfault.com/questions/562756/how-to-remove-the-path-with-an-nginx-proxy-pass/562850#562850 . 有关两个独立location声明的原理的更多详细信息,请参见https://serverfault.com/questions/562756/how-to-remove-the-path-with-an-nginx-proxy-pass/562850#562850

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM