大网数据广告活动
服务器运维问答

Nginx fails to start after boot, but works fine with a manual command

· 52CE 我爱测

I've recently got an Ubuntu 18 server and I'm a little confused to say the least. First: If I run sudo service nginx start or sudo systemctl start nginx , everything appears to work. My site is loading, and both sudo service ngin…

问题原文

原帖链接 · Mitchell van Zuylen · CC BY-SA 4.0

I've recently got an Ubuntu 18 server and I'm a little confused to say the least.

First: If I run sudo service nginx start or sudo systemctl start nginx, everything appears to work. My site is loading, and both sudo service nginx status and sudo systemctl status nginx return an all clear message.

Now the issue: When I reboot my server, nginx isn't automatically being started as the website is not being loaded. I tried sudo systemmctl enable nginx and tried another reboot. Again, nginx is not running as the website isn't loading.

In fact, if I reboot my server sudo reboot and then sudo service nginx status or sudo systemctl status nginx I get an error message. Despite the error message, I can run sudo service nginx start or sudo systemctl start nginx and it'll all go back to working.

The error message I get is the following:

(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.

Well, that seems pretty simple: it can't find the uwsgi_params file. THe uwsgi_params file does exist though, and it is in the right location.

My nginx .conf file:

# 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
    }
}

and the /data/project/uwsgi_params file is:

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;

Now, there's a few things I don't understand:

1) How is it possible that nginx works if I run the start command, but that nginx doesn't work on boot?

2) How can the status read that nginx can not find the uwsgi file, while it works on a start?

3) How can I get nginx to run properly on a reboot?

已采纳解答

原帖链接 · Lenniey · CC BY-SA 4.0

Nginx can't find the file because the mount / external filesystem is not ready at startup after boot. To tell nginx to wait for the mount, use systemctl edit nginx.service and add the following lines:

[Unit]
RequiresMountsFor=<mountpoint>

or in you specific case:

[Unit]
RequiresMountsFor=/data

nginx will now always wait for /data to be ready before starting.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。