是这样的,我有一台区服列表服务器,上面搭建有nginx服务,nginx安装目录在/usr/local/nginx下,我准备用system去管理nginx服务
cat /etc/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
做完后我启动nginx systemctl start nginx 发现了报错,执行systemctl status nginx
● nginx.service - The Nginx HTTP Server
Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2022-10-19 12:07:10 CST; 1 weeks 6 days ago
Process: 1494 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=1/FAILURE)
Oct 19 12:07:10 slsk_aliyun_prod_gameserverlist_1 systemd[1]: Starting The Nginx HTTP Server...
Oct 19 12:07:10 slsk_aliyun_prod_gameserverlist_1 nginx[1494]: nginx: [emerg] mkdir() "/tmp/nginx/cache" failed (2: No such file or directory)
Oct 19 12:07:10 slsk_aliyun_prod_gameserverlist_1 systemd[1]: nginx.service: control process exited, code=exited status=1
Oct 19 12:07:10 slsk_aliyun_prod_gameserverlist_1 systemd[1]: Failed to start The Nginx HTTP Server.
Oct 19 12:07:10 slsk_aliyun_prod_gameserverlist_1 systemd[1]: Unit nginx.service entered failed state.
Oct 19 12:07:10 slsk_aliyun_prod_gameserverlist_1 systemd[1]: nginx.service failed.
报错提示没有/tmp/nginx/cache 这个目录或文件,这个是nginx配置用来做缓存用的,缓存配置如下
proxy_cache_path /tmp/nginx/cache levels=2:2:2 use_temp_path=off keys_zone=tomcat_cache_zone:100m inactive=1h max_size=1g;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_cache tomcat_cache_zone;
proxy_cache_valid 200 302 1h;#200和302状态码保存1小时
proxy_cache_valid 301 1d;#301状态码保存一天
proxy_cache_valid any 1m;#其它的保存一分钟
proxy_pass http://slsk-portal;
}
我自己当时使用手动启动的nginx,直接进入到cd /usr/local/ngxin/sbin && ./nginx
然后我ll下,发现是有这个目录,当时就奇怪了,为什么用systemd方式无法启动呢
后面大佬跟我说是因为systemd unit中PrivateTmp 字段的原因,
只要使用Systemd这个进程作为启动进程的linux系统,其子进程都会有PrivateTmp这么一个属性,用于设置是否使用私有的tmp目录。
那么只要设置使用这个属性的service,都会使用私有的tmp目录。
比如说:
nginx会有一个/tmp/systemd-private-xxx-nginx.service/xxxx
使用PrivateTmp属性有什么好处呢,/tmp目录一般是所有用户和所有service都共享的,对于所有用户和service来说,都会有读和写的权限。
那么这里会存在安全性的问题,把各个service的tmp目录隔开的话,可以保证一定的安全性
对于每个service的tmp目录,会在服务启动时创建该目录,并且在关闭服务时删除该目录,如果我们是使用自己定义的临时路径的话,就需要我们自己定义相关的维护机制了,当我们在
/etc/systemd/system/nginx.service unit文件里设置了PrivateTmp=true 就代表该服务启动了该属性,所以为了不让这个启动报错,我们可以选择将该属性给去掉再次启动试试
服务器租用托管,机房租用托管,主机租用托管,https://www.e1idc.com