无法重新启动 nginx,我认为与端口 80 有关?

无法重新启动 nginx,我认为与端口 80 有关?

我正在关注 Corey Schafer 的 flask 教程。

我正在尝试部署到Linux服务器,但无法让nginx工作。

这些是我迄今为止运行过的命令:

sudo apt install nginx

pip install gunicorn

sudo rm /etc/nginx/sites-enabled/default

然后我尝试创建并编辑一个新文件:

sudo nano /etc/nginx/sites-enabled/flaskblog

这是我输入到该文件中的内容:

server {
    listen 80;
    server_name DOMAIN_IP;

    location /static {
        alias /Users/myname/VScode/Flask_Blog/flaskblog/static;
    }

    location / {
        proxy_pass http://localhost:8000;
        include /etc/nginx/proxy_params;
        proxy_redirect off;
    }
}

然后我跑

sudo nano ufw allow http/tcp

sudo ufw delete allow 5000

sudo ufw enable

然后当我尝试重新启动 nginx 时,我遇到了失败

sudo systemctl restart nginx

生产

Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.

systemctl status nginx.service

生成:

    nginx.service - A high performance web server and a reverse proxy server
         Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
         Active: failed (Result: exit-code) since Tue 2022-05-31 06:33:22 UTC; 4min 6s ago
           Docs: man:nginx(8)
        Process: 62279 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCC>
        Process: 62280 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)

sudo systemctl status nginx

Produces:

    nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2022-06-01 09:41:07 UTC; 2min 5s ago
       Docs: man:nginx(8)
    Process: 66304 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
        CPU: 11ms

Jun 01 09:41:07 flask-server systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 01 09:41:07 flask-server nginx[66304]: nginx: [emerg] unknown directive "/Users/charleserrington/VScode/Flask_Blog/flaskblog" in /etc/nginx/sites-enabled/flaskblog:6
Jun 01 09:41:07 flask-server nginx[66304]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jun 01 09:41:07 flask-server systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Jun 01 09:41:07 flask-server systemd[1]: nginx.service: Failed with result 'exit-code'.
Jun 01 09:41:07 flask-server systemd[1]: Failed to start A high performance web server and a reverse proxy server.

sudo nano /var/log/nginx/error.log

生成:

2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: still could not bind()
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: still could not bind()
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: still could not bind()
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: still could not bind()
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: still could not bind()
2022/06/01 05:54:41 [emerg] 65391#65391: bind() to 0.0.0.0:80 failed (98: Unknown error)

所有错误似乎都是未知的,所以我真的不知道从这里该去哪里。

我阅读了许多 serverfault 和 stackoverflow 帖子,似乎与端口 80 有关,我运行了许多推荐的命令,包括:

netstat -tnlp | grep -w 80
netstat -ano|grep 80|grep LISTEN
sudo netstat -lnp
sudo lsof -i :80
kill -9 65734 (i didn't have permission to run this)
netstat -tulpn
grep -rnw /etc/nginx/ -e '80'

其中一个命令使它工作起来并允许我重新启动 nginx 并访问网站,但我不知道哪个现在它又坏了,但上次看起来好像端口 80 有东西,但现在当我运行检查时似乎端口 80 上没有任何东西,所以我比以前更加迷茫。

编辑:

正如 Nick Andriopoulos 指出的那样,问题似乎是由

location /static {
    alias /Users/myname/VScode/Flask_Blog/flaskblog/static;
}

我把它改成了

location /static {
    root /Users/myname/VScode/Flask_Blog/flaskblog/;
}

完成此操作后,我能够重新启动 nginx,并且站点现在可以运行,但现在 nginx 无法找到静态文件。

我检查了错误日志,看到多行此类内容:

   2022/06/03 06:09:42 [error] 71064#71064: *1 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/main.css" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/main.css HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:42 [error] 71064#71064: *1 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/profile_pics/271fcf0ffe2a92aa.jpg" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/profile_pics/271fcf0ffe2a92aa.jpg HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:42 [error] 71064#71064: *4 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/profile_pics/default.jpg" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/profile_pics/default.jpg HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:45 [error] 71064#71064: *4 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/main.css" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/main.css HTTP/1.1", host: "IP", referrer: "http://IP/home"

因此 nginx 无法找到/访问我设置的路径。

但是,如果我在 Mac 上执行“转到文件夹”并粘贴,则所有文件和文件夹都在那里,这是正确的路径。感谢您的帮助。

答案1

从您的输出来看,该问题似乎与您的自定义配置有关:

sudo systemctl status nginx

Jun 01 09:41:07 flask-server nginx[66304]: nginx: [emerg] unknown directive "/Users/charleserrington/VScode/Flask_Blog/flaskblog" in /etc/nginx/sites-enabled/flaskblog:6
Jun 01 09:41:07 flask-server nginx[66304]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jun 01 09:41:07 flask-server systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE

这些错误意味着

    location /static {
        alias /Users/myname/VScode/Flask_Blog/flaskblog/static;
    }

最有可能是服务器无法启动的原因。绑定日志随后出现。

请确认上述路径()/Users/myname/VScode/Flask_Blog/flaskblog/static存在并且可以被nginx进程访问。

答案2

我认为端口 80 正在 Apache Web 服务器下运行。您无法同时运行两个 Web 服务器。

首先,检查端口 80 是否已运行,

sudo netstat -plunt | grep 80

tcp6   0   0    :::80    :::*   LISTEN      1573456/apache2

如果它正在运行,则禁用 apache:

sudo service apache2 stop

现在启动 Nginx:

sudo service nginx start

相关内容