当前位置:   article > 正文

nginx全局块的user指令_nginx #user

nginx #user

0、nginx默认访问的首页

file:///usr/local/nginx/html/index.html
  • 1

在这里插入图片描述

1、user指令

user:用于配置运行Nginx服务器worker进程 的用户和用户组

语法user user[group]
默认值nobody
位置全局块

该属性也可以在编译的时候指定,语法如下:

./configure --user=user
  • 1
./configure --group=group
  • 1

如果两个地方都进行了设置,最终生效的是配置文件中的配置。

1.1、进入nginx解压的目录

[root@localhost conf]# cd /opt/tool/nginx/nginx-1.20.1/
[root@localhost nginx-1.20.1]# pwd
/opt/tool/nginx/nginx-1.20.1
  • 1
  • 2
  • 3

1.2、./configure --help

[root@localhost nginx-1.20.1]# ./configure --help
  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes
  • 1
  • 2
  • 3
  • 4
  • 5

1.3、工作进程默认是nobody

[root@localhost nginx-1.20.1]# ps -ef | grep nginx
root       7337      1  0 18:12 ?        00:00:00 nginx: master process ./nginx
nobody     7338   7337  0 18:12 ?        00:00:00 nginx: worker process
root       7719   7193  0 18:48 pts/0    00:00:00 grep --color=auto nginx
  • 1
  • 2
  • 3
  • 4
[root@localhost nginx-1.20.1]# cat /usr/local/nginx/conf/nginx.conf

#user  nobody;
  • 1
  • 2
  • 3

2、user指令的使用步骤:

2.1、设置一个用户信息"www"

修改nginx.conf配置文件中的#user nobody;为user www;
在这里插入图片描述

user www;
  • 1
[root@localhost sbin]# pwd
/usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -t
nginx: [emerg] getpwnam("www") failed in /usr/local/nginx/conf/nginx.conf:2
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
  • 1
  • 2
  • 3
  • 4
  • 5

发现配置文件测试失败,这个时候我们需要创建一个用户www

2.2、 创建一个用户 useradd www

在这里插入图片描述

当我们添加一个用户时,home目录下会自动生成www目录

[root@localhost sbin]# useradd www
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  • 1
  • 2
  • 3
  • 4
[root@localhost sbin]# ps -ef | grep nginx
root       7337      1  0 18:12 ?        00:00:00 nginx: master process ./nginx
nobody     7338   7337  0 18:12 ?        00:00:00 nginx: worker process
root       8006   7193  0 19:12 pts/0    00:00:00 grep --color=auto nginx
  • 1
  • 2
  • 3
  • 4

2.3、./nginx -s reload

[root@localhost sbin]# ./nginx -s reload
[root@localhost sbin]# ps -ef | grep nginx
root       7337      1  0 18:12 ?        00:00:00 nginx: master process ./nginx
www        8016   7337  0 19:13 ?        00:00:00 nginx: worker process
root       8018   7193  0 19:13 pts/0    00:00:00 grep --color=auto nginx
  • 1
  • 2
  • 3
  • 4
  • 5

2.4、创建/root/html/index.html页面,添加如下内容

[root@localhost sbin]# cd /root/
[root@localhost ~]# mkdir html
[root@localhost ~]# cd html/
[root@localhost html]# vim index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans^Bserif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is
successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer
to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
<p><em>I am WWW</em></p>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

2.5、修改nginx.conf

location / {
root /root/html;
index index.html index.htm;
}
  • 1
  • 2
  • 3
  • 4
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# cd ../sbin/
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# ./nginx -s reload
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.6、测试启动访问

页面会报403拒绝访问的错误
在这里插入图片描述

2.7、分析原因

因为当前用户没有访问/root/html目录的权限

2.8、将文件创建到 /home/www/html/index.html

[root@localhost ~]# cd /home/www/
[root@localhost www]# ll
总用量 0
[root@localhost www]# cp -r /root/html/ ./
[root@localhost www]# ll
总用量 0
drwxr-xr-x. 2 root root 24 719 17:54 html
[root@localhost www]# cd html/
[root@localhost html]# ll
总用量 4
-rw-r--r--. 1 root root 603 719 17:54 index.html
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.9、修改配置 nginx.conf

location / {
root /home/www/html;
index index.html index.htm;
}
  • 1
  • 2
  • 3
  • 4

3.0、./nginx -s reload

[root@localhost conf]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# ./nginx -s reload
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

3.1、drwx------. 4 www www 90 7月 19 17:54 www

[root@localhost sbin]# cd /home/
[root@localhost home]# ll
总用量 4
drwx------. 14 dgq dgq 4096 710 14:26 dgq
drwx------.  4 www www   90 719 17:54 www
  • 1
  • 2
  • 3
  • 4
  • 5

3.2、dr-xr-x—. 18 root root 4096 7月 19 17:59 root

[root@localhost ~]# cd /
[root@localhost /]# ll
总用量 40
-rw-r--r--.   1 root root 2132 630 21:12 backups2.sql
-rw-r--r--.   1 root root 2132 630 21:00 backups.sql
lrwxrwxrwx.   1 root root    7 124 2023 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 124 2023 boot
drwxr-xr-x.  20 root root 3340 716 13:05 dev
drwxr-xr-x. 138 root root 8192 719 08:49 etc
drwxr-xr-x.   4 root root   28 717 19:11 home
lrwxrwxrwx.   1 root root    7 124 2023 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 124 2023 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 115 2016 media
drwxr-xr-x.   2 root root    6 115 2016 mnt
drwxr-xr-x.  11 root root 4096 74 20:18 opt
dr-xr-xr-x. 259 root root    0 716 13:05 proc
dr-xr-x---.  18 root root 4096 719 17:59 root
drwxr-xr-x.  42 root root 1240 719 08:50 run
lrwxrwxrwx.   1 root root    8 124 2023 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 115 2016 srv
dr-xr-xr-x.  13 root root    0 716 13:05 sys
drwxrwxrwt.  18 root root 4096 719 18:02 tmp
drwxr-xr-x.  13 root root  155 124 2023 usr
drwxr-xr-x.  21 root root 4096 124 2023 var
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

3、总结

综上所述,使用user指令可以指定启动运行工作进程的用户及用户组,
这样对于系统的权限访问控制的更加精细,也更加安全。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/894641
推荐阅读
相关标签
  

闽ICP备14008679号