在CentOS7上使用Gunicorn和Nginx部署Flask应用程序将导致502错误
我正在尝试使用Gunicorn和Nginx在CentOS7虚拟机上部署一个名为pytimesheets的flask应用程序。我基本上是跟随一些小的变化 当我使用在CentOS7上使用Gunicorn和Nginx部署Flask应用程序将导致502错误,nginx,flask,gunicorn,centos7,Nginx,Flask,Gunicorn,Centos7,我正在尝试使用Gunicorn和Nginx在CentOS7虚拟机上部署一个名为pytimesheets的flask应用程序。我基本上是跟随一些小的变化 当我使用gunicorn--bind 0.0.0.0:8080 wsgi:app(不使用Nginx)启动应用程序时,它可以工作,但这不是我希望它运行的方式 因此,我创建了一个systemd服务单元文件/etc/systemd/system/pytimesheets.service: [Unit] Description=Gunicorn inst
gunicorn--bind 0.0.0.0:8080 wsgi:app
(不使用Nginx)启动应用程序时,它可以工作,但这不是我希望它运行的方式
因此,我创建了一个systemd服务单元文件/etc/systemd/system/pytimesheets.service:
[Unit]
Description=Gunicorn instance to serve pytimesheets
After=network.target
[Service]
User=centos
Group=nginx
WorkingDirectory=/home/centos/pytimesheets/pytimesheets
Environment="PATH=/home/centos/miniconda/envs/pytimesheets-dev/bin"
ExecStart=/home/centos/miniconda/envs/pytimesheets-dev/bin/gunicorn --workers 3 --bind unix:pytimesheets.sock -m 007 wsgi:app
[Install]
WantedBy=mulit-user.target
已启动并启用gunicorn服务-已工作
接下来,我配置了Nginx:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 8080;
server_name my.servername.com;
location / {
proxy_set_header HOST $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/centos/pytimesheets/pytimesheets/pytimesheets.sock;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
我将用户nginx添加到我的用户组(centos,拥有sudo权限)sudo usermod-a-G user nginx
,并在我的主目录chmod 710/home/centos
最后,我启动了nginx,但当我访问该站点时,得到的只是一个502错误网关。
Nginx错误日志向我显示:
2016/07/06 13:33:44 [crit] 17464#0:
*1 connect() to unix:/home/centos/pytimesheets/pytimesheets/pytimesheets.sock
failed (13: Permission denied) while connecting to upstream,
client: 10.250.16.87, server: my.servername.com,
request: "GET / HTTP/1.1",
upstream: "http://unix:/home/centos/pytimesheets/pytimesheets/pytimesheets.sock:/",
host: "my.servername.com"
sock文件是在我启动pytimesheets服务后创建的,位置/home/centos/pytimesheets/pytimesheets/pytimesheets.sock
看起来很奇怪,但它是正确的
我的猜测是用户权限有问题。
有人知道我做错了什么吗
更新
设置
setenforce=Permissive
后,我能够运行该应用程序。但是,我不知道这是否是永久解决方案。gunicorn使用007创建文件,因此只有用户centos才能访问它。nginx以用户nginx的身份运行,因此无法访问007文件。将gunicorn执行更改为gunicorn[…]-m 077是否有效?不,仍然是相同的错误。奇怪的是,我看到了sock文件,但无法访问它。例如,cat pytimesheets.sock
返回cat:pytimesheets.sock:没有这样的设备或地址
,如果我试图用vim pytimesheets.sock
打开它,我会在vim中得到[权限被拒绝]
。我以centos和root的身份尝试了这一点。类似于ls-la
(或stat
或其他什么)的权限会显示在该文件上吗?ls-la
显示s-wxrw-rw-。1 centos nginx 0 Jul 7 09:31 pytimesheets.sock
也许它会帮助您gunicorn使用007创建文件,因此只有用户centos才能访问它。nginx以用户nginx的身份运行,因此无法访问007文件。将gunicorn执行更改为gunicorn[…]-m 077是否有效?不,仍然是相同的错误。奇怪的是,我看到了sock文件,但无法访问它。例如,cat pytimesheets.sock
返回cat:pytimesheets.sock:没有这样的设备或地址
,如果我试图用vim pytimesheets.sock
打开它,我会在vim中得到[权限被拒绝]
。我以centos和root的身份尝试了这一点。类似于ls-la
(或stat
或其他什么)的权限会显示在该文件上吗?ls-la
显示s-wxrw-rw-。1 centos nginx 0 Jul 7 09:31 pytimesheets.sock
也许对你有帮助