Nginx Not Port Listening作为服务运行时

时间:2016-04-05 03:41:54

标签: nginx centos

我在我的CentOS 7上配置Nginx。我可以通过命令运行nginx但不通过服务运行。我很感激任何帮助。

通过命令运行Nginx

当我用

启动nginx时
$ sudo nginx

我可以看到端口正在侦听,并且已成功连接到nginx与lynx。

$ 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

2 个答案:

答案 0 :(得分:1)

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

chcon -R -t httpd_config_t /etc/nginx 

应该修复它。如果没有,请检查/ var / log / audit以查看是否存在与SELinux相关的任何其他问题

答案 1 :(得分:0)

此答案特定于Docker。

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

原因是默认情况下守护程序工具(init.d,service,systemd)在Docker上不起作用。在Linux中,初始化进程必须具有PID1。但是,Docker不会将它们作为PID 1运行。PID 1被dumb-init -- sh -c ......占用,该CMD执行Docker配置文件中的CMD ["sh", "-c", "systemctl start nginx (or "service nginx start" etc.) && (your original command)"]语句。这就是为什么我的nginx不能作为服务启动的原因。

您可以“入侵” Docker以使用systemd,至少根据我在SO上阅读的内容,我认为这不是推荐的做法,或者您可以通过终端通过以下方式包含nginx start命令sorta:

SELECT * from translation WHERE translation.stop_name == stop.stop_name