手动启动nginx,静态HTML可以,但使用systemctl启动nginx,它是403



nginx version: nginx/1.19.0

我的nginx conf是/etc/nginx/nginx.conf

user  root;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
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"'  ' query_string=$query_string' ' port=$upstream_http_port' ' b=$arg_b';
access_log  /var/log/nginx/access.log  main;
sendfile        on;
#tcp_nopush     on;
keepalive_timeout  65;
#gzip  on;
include /etc/nginx/conf.d/*.conf;
server {
listen 10001;
server_name test1;
location / {
root /home/static/;
index index.html;
}
}
}

我的html文件是

$ cat /home/static/index.html
hello
-rw-r--r--. 1 root root 6 2021-10-07 05:38:24 static/index.html

当我启动nginx手动时,它就工作了。

$ nginx && curl localhost:10001
hello

但当我通过systemctl 启动时

$ systemctl start nginx && curl localhost:10001
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.0</center>
</body>
</html>

nginx.service状态,如

$ systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2021-10-07 07:34:00 CST; 1min 23s ago
Docs: http://nginx.org/en/docs/
Process: 13951 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=1/FAILURE)
Process: 14184 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 14185 (nginx)
CGroup: /system.slice/nginx.service
├─14185 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─14186 nginx: worker process
Oct 07 07:34:00 centos7 systemd[1]: Starting nginx - high performance web server...
Oct 07 07:34:00 centos7 systemd[1]: Started nginx - high performance web server.

这是我的nginx.service配置

cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target

对于那些拥有SELinux==Enforceing和Nginx的人,静态文件返回403:

启用SELinux httpdcannetwork_connect功能

sudo setsebool -P httpd_can_network_connect on 

检查SELinux强制

getenforce

如果是强制执行-

chcon -Rt httpd_sys_content_t /home/static/

最新更新