nginx全局块的user指令的实现示例

 更新时间:2024年07月18日 09:10:52   作者:小丁学Java  
user用于配置运行Nginx服务器的worker进程的用户和用户组,本文主要介绍了nginx全局块的user指令的实现示例,具有一定的参考价值,感兴趣的可以了解一下

1、user指令

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

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

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

./configure --user=user
./configure --group=group

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

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、./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.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
[root@localhost nginx-1.20.1]# cat /usr/local/nginx/conf/nginx.conf

#user  nobody;

2、user指令的使用步骤:

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

修改nginx.conf配置文件中的#user nobody;为user www;

在这里插入图片描述

user www;
[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

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

2.2、 创建一个用户

[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
[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

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

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/" rel="external nofollow" >nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/" rel="external nofollow" >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
<p><em>I am WWW</em></p>
</body>
</html>

在这里插入图片描述

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

2.5、修改nginx.conf

location / {
root /root/html;
index index.html index.htm;
}
[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

2.6、测试启动访问

页面会报403拒绝访问的错误

在这里插入图片描述

到此这篇关于nginx全局块的user指令的实现示例的文章就介绍到这了,更多相关nginx全局块user指令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 服务器的负载均衡nginx+tomcat实现动静分离

    服务器的负载均衡nginx+tomcat实现动静分离

    这篇文章主要为大家介绍了服务器的负载均衡nginx+tomcat实现动静分离的案例实施步骤以及环境详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03
  • Nginx配置SSL证书的全流程

    Nginx配置SSL证书的全流程

    文章详细介绍了如何通过阿里云或腾讯云申请免费SSL证书,并在Nginx中配置SSL以启用HTTPS,配置包括设置SSL会话缓存、超时、加密套件、优先级以及指定证书和密钥的位置,配置完成后,通过验证语法并重启Nginx,网站将启用HTTPS,用户访问时会看到浏览器地址栏的锁图标
    2025-02-02
  • CentOS7系统下用YUM安装Nginx详解

    CentOS7系统下用YUM安装Nginx详解

    相信大家都知道Nginx ("engine x") 是一个高性能的 HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。这篇文章将详细给大家介绍在CentOS7系统下用YUM安装Nginx的方法,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-11-11
  • Windows下Nginx + PHP5 的安装与配置方法

    Windows下Nginx + PHP5 的安装与配置方法

    Nginx 是一个轻量级的高性能 Http WebServer,以事件驱动方式编写,因此相比 Apache 而言,Nginx 更加稳定、性能更好,而且配置简单,资源占用较低。以下是我在 Windows 7 安装中 Nginx 和 PHP5.3 的步骤。
    2010-07-07
  • 利用nginx搭建RTMP视频点播、直播、HLS服务器

    利用nginx搭建RTMP视频点播、直播、HLS服务器

    本文主要介绍了利用nginx搭建RTMP视频点播、直播、HLS服务器,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 使用nginx实现一个端口和ip访问多个vue前端的全过程

    使用nginx实现一个端口和ip访问多个vue前端的全过程

    为满足单端口访问多个前端应用的需求,需要对nginx进行配置,同时修改vue项目的publicPath参数,这篇文章主要介绍了使用nginx实现一个端口和ip访问多个vue前端的相关资料,需要的朋友可以参考下
    2024-09-09
  • nginx安装并转发socket服务实现方式

    nginx安装并转发socket服务实现方式

    文章指导如何安装编译工具、PCRE和Nginx,并通过修改配置文件添加stream块实现端口转发,将9999/801端口分别映射至9988/1557端口
    2025-07-07
  • 使用Docker实现Nginx反向代理

    使用Docker实现Nginx反向代理

    本文主要介绍了使用Docker实现Nginx反向代理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • 为什么 Nginx 比 Apache 更牛

    为什么 Nginx 比 Apache 更牛

    为什么Nginx在处理高并发方面要优于httpd,我们先从两种web服务器的工作原理以及工作模式说起。对Nginx对比 Apache的相关知识感兴趣的朋友跟随小编一起看看吧
    2021-02-02
  • 通过Nginx实现前端与后端的协同部署

    通过Nginx实现前端与后端的协同部署

    在现代 web 开发中,前端与后端的协同部署是一个关键问题,一个高效的部署策略不仅能提升用户体验,还能简化开发流程,今天,我们就来探讨如何利用 Nginx 实现前端与后端的协同部署,需要的朋友可以参考下
    2025-03-03

最新评论