簡體   English   中英

Nginx 作為服務運行時不監聽端口

[英]Nginx Not Port Listening when it runs as service

我正在 CentOS 7 上配置 Nginx。我可以通過命令運行 nginx,但不能通過服務運行。 我很感激任何幫助。

通過命令運行 Nginx

當我啟動 nginx 時

$ sudo nginx

我可以看到端口正在偵聽,並且我已經成功地使用 lynx 連接到了 nginx。

$ netstat -nap | grep 8000
(No info could be read for "-p": geteuid()=1000 but you should be root.)
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      -                   

wget 也沒有問題,

$ wget http://127.0.0.1:8000
--2016-04-05 13:33:01--  http://127.0.0.1:8000/
Connecting to 127.0.0.1:8000... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.2’

    [ <=>                                                                                 ] 11          --.-K/s   in 0s      

2016-04-05 13:33:01 (1.53 MB/s) - ‘index.html.2’ saved [11]

通過 Systemd 運行 Nginx

但是,當我通過 systemd 啟動 nginx 時

$ sudo systemctl start nginx

8000 端口沒有監聽。

$ netstat -nap | grep 8000
(No info could be read for "-p": geteuid()=1000 but you should be root.)

這是 wget 的結果

$ wget http://127.0.0.1:8000
--2016-04-05 13:34:52--  http://127.0.0.1:8000/
Connecting to 127.0.0.1:8000... failed: Connection refused.

我檢查了錯誤日志(/var/log/nginx/error.log),

Apr  5 12:57:24 localhost systemd: Starting The NGINX HTTP and reverse proxy server...
Apr  5 12:57:24 localhost nginx: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr  5 12:57:24 localhost nginx: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr  5 12:57:24 localhost systemd: Failed to read PID from file /var/run/nginx.pid: Invalid argument
Apr  5 12:57:24 localhost systemd: Started The NGINX HTTP and reverse proxy server.

配置文件已通過測試

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

這是主要的配置文件/etc/nginx/nginx.conf

$ cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

這是 nginx 配置文件/etc/nginx/conf.d/test_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server 0.0.0.0:8001; 
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;

    # the domain name it will serve for
    server_name 0.0.0.0; # substitute your machine's IP address or FQDN
    charset     utf-8;

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

    location /static {
        alias /src/frontend/DjangoServer/static;
    }

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

這是 nginx systemd 配置文件

$ cat /etc/systemd/system/nginx.service 
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

可能 SELinux 不允許 nginx 讀取 /etc/nginx/sites-enabled/ 下的配置,從另一個站點復制配置時我遇到了同樣的問題。

chcon -R -t httpd_config_t /etc/nginx 

應該修復它。 如果沒有檢查 /var/log/audit 看看是否還有其他與 SELinux 相關的問題

此答案特定於 Docker。

我遇到了同樣的問題,我可以通過命令運行 nginx 但不能通過服務。 在 Docker (Debian) 上。

原因是守護程序工具(init.d、service、systemd)默認在 Docker 上不起作用。 在 Linux 中,init 進程必須有 PID 1。但是,Docker 不會將它們作為 PID 1 運行。PID 1 被dumb-init -- sh -c ......占用dumb-init -- sh -c ......它在你里面執行CMD語句Docker 配置文件。 這就是為什么我的 nginx 沒有作為服務啟動。

您可以“破解” Docker 以使用 systemd,至少根據我在 SO 上閱讀的內容,我認為這不是推薦的做法,或者您可以通過終端包含 nginx start 命令 sorta,如下所示:

CMD ["sh", "-c", "systemctl start nginx (or "service nginx start" etc.) && (your original command)"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM