Nginx从特定路径向另一个域提供Django(uWSGI套接字)

时间:2016-11-22 16:09:49

标签: django nginx uwsgi wsgi

目前有uWSGI调解nginx和Django之间的请求和响应。有两个站点A和B:

  • A)alpha.com
  • B)www.beta.com

现在,我想通过将{nginx指向alpha.com/awards处的B&#uWSGI套接字,从路径/home/web/beta/uwsgi/site.sock提供网站B,以便可以从www.beta.com/one访问网页P1可以从alpha.com/awards/onewww.beta.com/two访问alpha.com/awards/two或P2。

网址在django中定义,而不是nginx中的静态资源。

我该怎么做?

配置文件:

# /etc/nginx/sites-available/alpha

server {
    listen 80;
    listen 443 ssl spdy;

    server_name www.alpha.com;

    # Set certificate files.
    ssl_certificate /etc/nginx/cert/alpha.cert;
    ssl_certificate_key /etc/nginx/cert/alpha.key;

    return 301 $scheme://alpha.com$request_uri;
}

server {
    listen 443 ssl spdy;

    server_name alpha.com;

    # Set certificate files.
    ssl_certificate /etc/nginx/cert/alpha.cert;
    ssl_certificate_key /etc/nginx/cert/alpha.key;

    # Rewrite all news articles to HTTP, because comments don't work in HTTPS.
    #rewrite "^/(news/\d+/\d+/\d+/.*)$" http://$http_host/$1 redirect;

    include sites-available/alpha.shared;
}



# /etc/nginx/sites-available/alpha.shared

root /usr/share/nginx/www;
index index.html index.htm;

# Use the custom error page.
error_page 500 /error/500.html;
error_page 502 /error/502.html;

# Serve custom error pages from the Django templates directory.
location ^~ /error/ {
    internal;
    alias /home/web/alpha/templates/;
}

location / {
    include uwsgi_params;
    uwsgi_pass unix:/home/web/alpha/uwsgi/site.sock;
}

# Set up all static files.
location /robots.txt { 
    alias /home/web/alpha/static/robots.txt; 
}

location /static/css/ { 
    include expires_headers;
    alias /home/web/alpha/static/css/; 
}


# /etc/nginx/sites-available/beta
server {
    listen 80;
    listen 443 ssl spdy;

    server_name beta.co.uk;

    # Set certificate files.
    ssl_certificate /etc/nginx/cert/2015-beta.cert;
    ssl_certificate_key /etc/nginx/cert/2015-beta.key;

    return 301 $scheme://www.beta.co.uk$request_uri;
}
server {
    listen 443 ssl spdy;

    # Serve from www.
    server_name www.beta.co.uk;

    # Set certificate files.
    ssl_certificate /etc/nginx/cert/2016-beta.crt;
    ssl_certificate_key /etc/nginx/cert/2016-beta.key;

    include sites-available/beta.shared;
}
server {
    listen 80;

    # Serve from www.
    server_name www.beta.co.uk;

    # Redirect all HTTP traffic to HTTPS
    return 302 https://www.beta.co.uk$request_uri;
}


# /etc/nginx/sites-available/beta.shared
root /usr/share/nginx/www;
index index.html index.htm;

error_page 500 /error/500.html;
error_page 502 /error/502.html;

# Serve custom error pages from the Django templates directory.
location ^~ /error/ {
    internal;
    alias /home/web/beta/templates/;
}

location / {
    include uwsgi_params;
    uwsgi_pass unix:/home/web/beta/uwsgi/site.sock;
}

# Set up all static files.
location /static/css/ { 
    include expires_headers;
    alias /home/web/beta/static/css/; 
}

1 个答案:

答案 0 :(得分:0)

我完全不确定这会对会话处理产生什么影响,但你可以试试这个:

# /etc/nginx/sites-available/alpha.shared

root /usr/share/nginx/www;
index index.html index.htm;

# Use the custom error page.
error_page 500 /error/500.html;
error_page 502 /error/502.html;

# Serve custom error pages from the Django templates directory.
location ^~ /error/ {
    internal;
    alias /home/web/alpha/templates/;
}

location /awards/one {        
    include uwsgi_params;
    uwsgi_pass unix:/home/web/alpha/uwsgi/site.sock;
}

location /awards/two {
    rewrite /awards(.*)$ $1 break;
    proxy_pass https://www.beta.co.uk;
}

# Set up all static files.
location /robots.txt { 
    alias /home/web/alpha/static/robots.txt; 
}

location /static/css/ { 
    include expires_headers;
    alias /home/web/alpha/static/css/; 
}