技术解析

NGINX 绑定域名到 xxx.com,如何同时绑定到 xxx.com/en/?
0
2021-05-25 18:09:07
idczone

自己写了一个网站,绑定域名 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
    }
}

```
rewrite ^/en/(.*) /$1;
```

location {
rewrite ^/en/(.*) /$1;
}

location {
rewrite ^/en/(.*) /$1 break;
}

如果网站的内容很少变动的话,我认为最简单的方法是把网站在./en/也放一份。

查一下 nginx 的 rewrite

数据地带为您的网站提供全球顶级IDC资源