Nginx 启动后无法启动,但使用手动命令可以正常工作

Nginx 启动后无法启动,但使用手动命令可以正常工作

我最近得到了一个 Ubuntu 18 服务器,至少可以说我有点困惑。

首先:如果我运行sudo service nginx startsudo systemctl start nginx,一切似乎都正常。我的网站正在加载,并且 和sudo service nginx statussudo systemctl status nginx返回一条全部清除的消息。

现在的问题是:当我重启服务器时,nginx由于网站没有加载,所以它不会自动启动。我试了sudo systemmctl enable nginx又试了一次重启。同样,nginx由于网站没有加载,所以它没有运行。

事实上,如果我重启服务器sudo reboot然后sudo service nginx statussudo systemctl status nginx出现错误消息。尽管出现错误消息,我仍然可以运行sudo service nginx start或,sudo systemctl start nginx然后一切将恢复正常。

我收到的错误消息如下:

(venv) user@server:/data/project$ sudo systemctl status nginx
● 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 2019-02-26 17:50:18 CET; 1min 6s ago
     Docs: man:nginx(8)
  Process: 830 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)

Feb 26 17:50:18 md3.tudelft.nl systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 26 17:50:18 md3.tudelft.nl nginx[830]: nginx: [emerg] open() "/data/project/uwsgi_params" failed (2: No such file or directory) in /etc/nginx/sites-enabled/paintingViewer.conf
Feb 26 17:50:18 md3.tudelft.nl nginx[830]: nginx: configuration file /etc/nginx/nginx.conf test failed
Feb 26 17:50:18 md3.tudelft.nl systemd[1]: nginx.service: Control process exited, code=exited status=1
Feb 26 17:50:18 md3.tudelft.nl systemd[1]: nginx.service: Failed with result 'exit-code'.
Feb 26 17:50:18 md3.tudelft.nl systemd[1]: Failed to start A high performance web server and a reverse proxy server.

嗯,这似乎很简单:它找不到 uwsgi_params 文件。但是 uwsgi_params 文件确实存在,并且位于正确的位置。

我的nginx.conf文件:

# the upstream component nginx needs to connect to
upstream django {
     server unix:///data/project/paintingViewer.sock; # for a file socket
    # server 127.0.0.1:8001; # for a web port socket -> works if we use Djano's build in webserver.
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name server;
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /data/project/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /data/project/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /data/project/uwsgi_params; # the uwsgi_params file you installed
    }
}

/data/project/uwsgi_params文件为:

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

现在,有几件事我不明白:

nginx1)如果我运行start命令,但它nginx在启动时不起作用,怎么可能呢?

status2)当读取文件在 上工作时,如何nginx找到该文件?uwsgistart

3)如何nginx在重启后正常运行?

答案1

Nginx 找不到该文件,因为启动后挂载/外部文件系统尚未准备好。要告诉 nginx 等待挂载,请使用systemctl edit nginx.service并添加以下几行:

[Unit]
RequiresMountsFor=<mountpoint>

或者在你的具体情况下:

[Unit]
RequiresMountsFor=/data

nginx 现在将始终等待/data准备就绪后再启动。

相关内容