Nginx 从特定路径为另一个域的 Django(uWSGI 套接字)提供服务
Posted
技术标签:
【中文标题】Nginx 从特定路径为另一个域的 Django(uWSGI 套接字)提供服务【英文标题】:Nginx to serve Django (uWSGI socket) from specific path with another domain 【发布时间】:2017-04-06 09:46:06 【问题描述】:目前有 uWSGI 调解 nginx 和 Django 之间的请求和响应。有两个站点 A 和 B:
A) alpha.com B) www.beta.com现在我想通过将 nginx 指向位于 /home/web/beta/uwsgi/site.sock
的 B 的 uWSGI 套接字,从路径 alpha.com/awards
为站点 B 提供服务,这样可以从 www.beta.com/one
和 alpha.com/awards/one
访问页面 P1,或者可以从www.beta.com/two
和 alpha.com/awards/two
。
url 是在 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】:我完全不确定这会对会话处理产生什么影响,但你可以试试这个:
# /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/;
【讨论】:
以上是关于Nginx 从特定路径为另一个域的 Django(uWSGI 套接字)提供服务的主要内容,如果未能解决你的问题,请参考以下文章