V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
97world
V2EX  ›  NGINX

NGINX 绑定域名到 xxx.com,如何同时绑定到 xxx.com/en/?

  •  1
     
  •   97world · 2015-11-29 21:05:58 +08:00 · 3436 次点击
    这是一个创建于 3076 天前的主题,其中的信息可能已经有所发展或是发生改变。

    自己写了一个网站,绑定域名 xxx.com ,希望通过 xxx.com/en/也可以访问.

    例如 xxx.com/product/detail.html 这个页面,用 xxx.com/en/product/detail.html 也能够访问,希望能够做到这样的效果.
    因为是用 Django 写的,应该也是可以用路由配置来达到这个效果,不过觉得丢给 NGINX 去做可能更清晰一点.

    环境是 Python+Django+uWSGI+NGINX. 下面是我 NGINX 的配置,请帮我看一下吧.

    # the upstream component nginx needs to connect to
    upstream XXX_EN {
        server unix:///home/wwwroot/PY_XXX_EN/web.sock; # for a file socket
    }
    
    # configuration of the server
    server {
        # the port your site will be served on
        listen      80;
        # the domain name it will serve for
        server_name     en.XXX.com;
    
        # max upload size
        client_max_body_size 75M;   # adjust to taste
    
        location /cn {
            rewrite ^/ http://www.XXX.com/;
        }
    
        location /static {
            alias /home/wwwroot/STATIC_XXX_EN/static;
            expires 12h;
        }
    
    
        location /en {
            uwsgi_pass  XXX_EN;
            include /usr/local/nginx/conf/uwsgi_params;
        }
    
        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  XXX_EN;
            include     /usr/local/nginx/conf/uwsgi_params; # the uwsgi_params file you installed
        }
    }
    

    我用下面的配置来实现我说的效果,但是无效.

    location /en {
        uwsgi_pass  XXX_EN;
        include /usr/local/nginx/conf/uwsgi_params;
    }
    
    5 条回复    2015-11-30 09:46:17 +08:00
    eternityz
        1
    eternityz  
       2015-11-29 21:10:57 +08:00
    ```
    rewrite ^/en/(.*) /$1;
    ```
    binnchx
        2
    binnchx  
       2015-11-29 21:26:15 +08:00
    location {
    rewrite ^/en/(.*) /$1;
    }
    binnchx
        3
    binnchx  
       2015-11-29 21:26:31 +08:00
    location {
    rewrite ^/en/(.*) /$1 break;
    }
    JJaicmkmy
        4
    JJaicmkmy  
       2015-11-30 08:16:46 +08:00 via iPad
    如果网站的内容很少变动的话,我认为最简单的方法是把网站在./en/也放一份。
    zhangv
        5
    zhangv  
       2015-11-30 09:46:17 +08:00
    查一下 nginx 的 rewrite
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2414 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:08 · PVG 08:08 · LAX 17:08 · JFK 20:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.