linux使用nginx配置后没有用

我用flask框架写好一个网站后,部署到了云服务器上,已经用gunicorn部署好了,但是只能访问程序设定的2000端口,现在想通过访问默认80端口,就能跳转到2000端口,我使用了nginx,但是当我配置好后发现怎么都不管用,具体如下

配置文件如下

server{
    listen 80;
    location / {
        proxy_pass http://localhost:2000;
    }
}

我将配置文件做了软链接,linux显示如下:

ll /etc/nginx/sites-enabled/bbs
/etc/nginx/sites-enabled/bbs -> /var/www/bbs/bbs.nginx

bbs.nginx就是配置文件,然后我重启nginx

server nginx restart

我接下来访问主页时出错了,主页显示403错误,主页后面的具体某一页显示404错误
也可能是因为关闭了gunicorn,因为当我手动开启gunicron时,代码如下

gunicorn wsgi --bind 0.0.0.0:2000 --pid /tmp/bbs.pid

这时候shell上只显示

[2018-05-20 01:12:31 +0800] [1311] [INFO] Starting gunicorn 19.8.1
[2018-05-20 01:12:31 +0800] [1311] [INFO] Listening at: http://0.0.0.0:2000 (1311)
[2018-05-20 01:12:31 +0800] [1311] [INFO] Using worker: sync
[2018-05-20 01:12:31 +0800] [1314] [INFO] Booting worker with pid: 1314

在这种情况下我没办法输入别的命令,只能按Ctrl+任意键退出,退出提示如下

[1]  + 1311 suspended  gunicorn wsgi --bind 0.0.0.0:2000 --pid /tmp/bbs.pid

这时候我才能输入别的命令,请大佬帮我指出错误或者给我这个小白点建议,不胜感激

阅读 3.2k
2 个回答

后台运行命令,不影响终端输入,可以用 & 符号在最后。如:

gunicorn wsgi --bind 0.0.0.0:2000 --pid /tmp/bbs.pid &

重启服务用service,不是server!

service nginx restart

重启之前要用下面的指令检查配置文件是否正确:

service nginx configtest

启动之后要检查/var/log/nginx/error.log, 看有什么出错信息没有。

首先,你的部署思路没搞清楚。

下面,用一个最简单的例子,来尝试说明。

场景设置:

操作系统:ubuntu 18.04 LTS
不考虑防火墙和AppArmor
python环境使用pythonenv进行管理,路径为/home/username/.virtualenvs/hello
假定我的Flask应用如下/home/username/project/hello.py(摘自Flask官方文档):

# hello.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

问题:如何使gunicorn在后台运行(daemonize)?

直接在命令行运行gunicorn,只适合开发或者debug场景,生产环境要求gunicorn必须在后台运行(daemonize)。
当然,你可以直接在terminal下运行下面的命令,强制gunicorn以后台模式(&)运行。

gunicorn --bind /run/gunicorn/socket --pid /run/gunicorn/pid hello:app &

但是,这种模式有先天缺陷,比如使得gunicorn依赖于运行它的terminal,从可靠性(比如当运行它的terminal进程挂了,gunicorn也就挂了)和可用性(比如系统重启后,gunicorn无法自动运行)等角度,都使这种方法不适合在生产环境中使用。
备注gunicorn--bind参数值设置为/run/gunicorn/socket,而不是0.0.0.0:2000,因为unix socket性能高于tcp port,当nginxgunicorn不在同一主机时,则只能用tcp port

解决办法之一systemd

创建下述文件/etc/systemd/system/gunicorn.service,内容如下:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
PIDFile=/run/gunicorn/pid # PID文件
User=www-data # 进程所属用户
Group=www-data # 进程所属用户组
RuntimeDirectory=gunicorn # 在/run目录下创建的目录名称
WorkingDirectory=/home/username/project # 网站根目录,根据情况进行调整
ExecStart=/home/username/.virtualenvs/hello/bin/gunicorn --pid /run/gunicorn/pid \
          --bind unix:/run/gunicorn/socket hello:app # gunicorn命令,路径根据情况进行调整
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

运行如下命令启动gunicorn服务并使其开机自动启动。

sudo systemctl enable --now gunicorn.service

运行如下命令查看gunicorn服务状态。

sudo systemctl status gunicorn.service

备注:除了systemd之外,还可以使用supervisor来管理gunicorn(自行Google)。

nginx设置

server {
    listen 80;
    server_name www.yourhostname.com;
    location / {
        proxy_pass http://unix:/run/gunicorn/socket;
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题