为两个 Django 应用正确定义 Nginx 服务器块,没有 server_name
Defining correctly Nginx server block for two django apps and no server_name
我一直在关注 Digital Ocean 教程 How To Serve Django Applications with uWSGI and Nginx on Ubuntu 14.04,以便以后我可以使用 Nginx+uWSGI 部署我自己的 django 应用程序。
在本教程中,他们创建了 2 个基本的 Django 应用程序,稍后将由 Nginx 提供服务。我已经测试过这些应用程序可以单独使用 Django 服务器和 uWSGI。
当我传递到 Nignx 部分时,我 运行 遇到了问题,基本上我没有 server_name 现在只有一个 IP 可以使用,我试图区分 Django 应用程序使用端口号。
默认的 Nginx 服务器 (xxx.xxx.xxx.xxx:80) 响应正确,但是当我尝试使用 (xxx.xxx.xxx.xxx:8080 或 xxx.xxx.xxx.xxx:8081) 访问 Django 应用程序时,我获取 502 错误网关。
我认为我在服务器块内定义 listen 的方式或逻辑有问题。这样做的正确方法是什么,或者我可能做错了什么。
这是我的服务器块(在启用站点的情况下):
第一站点应用程序
server {
listen xxx.xxx.xxx.xxx:8080;
#server_name _;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/firstsite;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/root/firstsite/firstsite.sock;
}
}
第二站点应用程序
server {
listen xxx.xxx.xxx.xxx:8081;
#server_name _;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/secondsite;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/root/secondsite/secondsite.sock;
}
}
默认 Nginx
server {
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
更新:
我正在检查 /var/log/nginx 下的错误日志,当我尝试连接到第一个站点时,我收到以下错误:
2016/02/05 15:55:23 [crit] 11451#0: *6 connect() to
unix:/root/firstsite/firstsite.sock failed (13: Permission denied)
while connecting to upstream, client: 188.37.180.101, server: ,
request: "GET / HTTP/1.1", upstream:
"uwsgi://unix:/root/firstsite/firstsite.sock:", host:
"178.62.229.183:8080"
默认情况下,ubuntu 上的 Nginx 服务器将 运行 上 www-data
用户,uWSGI 服务器不会(这实际上是一件好事,除非它 运行s在根上)。如果你正在为 uWSGI 创建 unix 套接字,那么对它的访问将被定义为任何系统文件。默认情况下,对它的访问可能仅限于创建套接字的用户。
更多信息,您正在 /root/
目录中创建套接字。该目录只能由 root 用户读取,一些 Linux 发行版将不允许访问其中的任何内容,即使权限设置正确也是如此。
所以你要做的是:
- 将套接字放在
/root/
目录之外(/var/run
是个好地方)
- 确保 nginx 可以访问该套接字(将
--chmod-socket 666
或 `--chown-socket yourusername:www-data 放入您的 uWSGI 启动行)
并且如果您运行在 root 上安装 uWSGI 服务器,请注意这真的很危险。 root 上的任何进程 运行ning 都可以对你的系统做任何事情,所以如果你的代码出错或者有人入侵,他可以将任何恶意软件注入你的服务器,从中窃取一些数据或者只是破坏一切。
我一直在关注 Digital Ocean 教程 How To Serve Django Applications with uWSGI and Nginx on Ubuntu 14.04,以便以后我可以使用 Nginx+uWSGI 部署我自己的 django 应用程序。
在本教程中,他们创建了 2 个基本的 Django 应用程序,稍后将由 Nginx 提供服务。我已经测试过这些应用程序可以单独使用 Django 服务器和 uWSGI。
当我传递到 Nignx 部分时,我 运行 遇到了问题,基本上我没有 server_name 现在只有一个 IP 可以使用,我试图区分 Django 应用程序使用端口号。
默认的 Nginx 服务器 (xxx.xxx.xxx.xxx:80) 响应正确,但是当我尝试使用 (xxx.xxx.xxx.xxx:8080 或 xxx.xxx.xxx.xxx:8081) 访问 Django 应用程序时,我获取 502 错误网关。
我认为我在服务器块内定义 listen 的方式或逻辑有问题。这样做的正确方法是什么,或者我可能做错了什么。
这是我的服务器块(在启用站点的情况下):
第一站点应用程序
server { listen xxx.xxx.xxx.xxx:8080; #server_name _; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /root/firstsite; } location / { include uwsgi_params; uwsgi_pass unix:/root/firstsite/firstsite.sock; } }
第二站点应用程序
server { listen xxx.xxx.xxx.xxx:8081; #server_name _; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /root/secondsite; } location / { include uwsgi_params; uwsgi_pass unix:/root/secondsite/secondsite.sock; } }
默认 Nginx
server { listen 80 default_server; #listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } }
更新:
我正在检查 /var/log/nginx 下的错误日志,当我尝试连接到第一个站点时,我收到以下错误:
2016/02/05 15:55:23 [crit] 11451#0: *6 connect() to unix:/root/firstsite/firstsite.sock failed (13: Permission denied) while connecting to upstream, client: 188.37.180.101, server: , request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/root/firstsite/firstsite.sock:", host: "178.62.229.183:8080"
默认情况下,ubuntu 上的 Nginx 服务器将 运行 上 www-data
用户,uWSGI 服务器不会(这实际上是一件好事,除非它 运行s在根上)。如果你正在为 uWSGI 创建 unix 套接字,那么对它的访问将被定义为任何系统文件。默认情况下,对它的访问可能仅限于创建套接字的用户。
更多信息,您正在 /root/
目录中创建套接字。该目录只能由 root 用户读取,一些 Linux 发行版将不允许访问其中的任何内容,即使权限设置正确也是如此。
所以你要做的是:
- 将套接字放在
/root/
目录之外(/var/run
是个好地方) - 确保 nginx 可以访问该套接字(将
--chmod-socket 666
或 `--chown-socket yourusername:www-data 放入您的 uWSGI 启动行)
并且如果您运行在 root 上安装 uWSGI 服务器,请注意这真的很危险。 root 上的任何进程 运行ning 都可以对你的系统做任何事情,所以如果你的代码出错或者有人入侵,他可以将任何恶意软件注入你的服务器,从中窃取一些数据或者只是破坏一切。