【问题标题】:Nginx Not Port Listening when it runs as serviceNginx 作为服务运行时不监听端口
【发布时间】:2019-01-25 11:09:46
【问题描述】:

我正在我的 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

【问题讨论】:

    标签: nginx centos


    【解决方案1】:

    可能 SELinux 不允许 nginx 读取 /etc/nginx/sites-enabled/ 下的配置,我从另一个站点复制配置时遇到了同样的问题。

    chcon -R -t httpd_config_t /etc/nginx 
    

    应该修复它。如果不检查 /var/log/audit 看看是否有其他与 SELinux 相关的问题

    【讨论】:

    • 蹩脚的 SELinux,我每次都要禁用它:(
    • 想知道是否有 systemd 配置文件指令来执行此操作而不是 chcon 命令。
    【解决方案2】:

    这个答案是特定于 Docker 的。

    我遇到了同样的问题我可以通过命令运行 nginx,但不能通过服务运行。 在 Docker (Debian) 上。

    原因是守护程序工具(init.d、service、systemd)默认不能在 Docker 上运行。在 Linux 中,init 进程必须具有 PID 1。但是,Docker 不会将它们作为 PID 1 运行。PID 1 被 dumb-init -- sh -c ...... 占用,它执行 Docker 配置文件中的 CMD 语句。这就是为什么我的 nginx 没有作为服务启动的原因。

    您可以“破解” Docker 以使用 systemd,至少根据我在 SO 上阅读的内容,我认为这不是推荐的做法,或者您可以通过终端包含 nginx start 命令排序,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多