Nginx降权启动

        给Nginx服务降权,用lol用户跑Nginx,给开发及运维设置普通账号,只要和lol同组即可管理Nginx,方案解决了Nginx管理问题,防止root分配权限过大。

开发人员使用普通账户即可管理Nginx站点以下程序问题。采取项目负责制制度,谁负责项目维护了问题谁负责。

1. 更改nginx默认用户及用户组(worker进程优化)

a.建立nginx用户
[root@mysql-db01 conf]# useradd www -s /sbin/nologin -M useradd: user 'nginx' already exists
b.查询nginx用户 [root@mysql
-db01 conf]# id nginx uid=497(nginx) gid=498(nginx) groups=498(nginx) [root@mysql-db01 conf]#

2.配置nginx.conf使用nginx用户

user  www www;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
.........

也可以编译的时候指定用户.

[root@mysql-db01 conf]# /application/nginx/sbin/nginx -V
nginx version: nginx/1.10.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.10.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

3.  让nginx运行于监牢模式

 这种方式简单来说就是为master服务降权:使用非root跑nginx master

注意:不能用80特权端口,前端nginx反向代理转端口在启动的时候指定配置文件,普通用户只能只用1024以上的端口。

[root@web01 ~]# useradd lol   ##创建普通用户
[root@web01 ~]# ll -ld /home/lol/
drwx------ 2 inca inca 4096 Jul 31 18:19 /home/inca/
[root@web01 ~]# 
[root@web01 ~]# su - lol    ##切换到普通用户下来进行下步操作

之所以要进入到普通用户下操作,是让普通用户对自己所创建的文件具有所有权

[inca@web01 ~]$ pwd
/home/inca
[inca@web01 ~]$ mkdir conf  logs  www   ##创建必须的配置文件,日志,站点目录
[inca@web01 ~]$ cp /application/nginx/conf/mime.types ~/conf/    #拷贝配置文件中网页支持类型文件
[inca@web01 ~]$ echo inca >www/index.html

拷贝原有配置文件nginx.conf并修改目录。

[inca@www ~]$ cat conf/nginx.conf 
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535;
error_log  /home/lol/logs/error.log;
user www www;     
pid        /home/lol/logs/nginx.pid;
events {
    use epoll;
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    #web.fei fa dadong..............
    server {
        listen       8080;
        server_name  www.dadong.org 

;
        root   /home/lol/www;
        location / {
            index  index.php index.html index.htm;
                }
         access_log  /home/lol/logs/web_blog_access.log  main;
           }
}

 使用指定配置文件的方式启动nginx

[lol@web01 conf]$ /data/nginx/sbin/nginx -c /home/lol/conf/nginx.conf 
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /home/lol/conf/nginx.conf:5
##此处有警告,但是不影响nginx启动操作,如果有哪位能告诉我如何去除警告,先谢谢了。
[lol@mysql
-db01 conf]$ ps -ef |grep nginx lol 38719 1 0 23:15 ? 00:00:00 nginx: master process /data/nginx/sbin/nginx -c /home/lol/conf/nginx.conf lol 38720 38719 0 23:15 ? 00:00:00 nginx: worker process lol 38721 38719 0 23:15 ? 00:00:00 nginx: worker process lol 38722 38719 0 23:15 ? 00:00:00 nginx: worker process lol 38723 38719 0 23:15 ? 00:00:00 nginx: worker process lol 38725 38685 0 23:15 pts/1 00:00:00 grep nginx

[lol@mysql-db01 conf]$ curl -I 10.0.0.51:8080
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Mon, 06 Nov 2017 15:34:21 GMT
Content-Type: text/html
Content-Length: 5
Last-Modified: Mon, 06 Nov 2017 15:11:58 GMT
Connection: keep-alive
ETag: "5a007bbe-5"
Accept-Ranges: bytes

[lol@mysql-db01 conf]$ curl 10.0.0.51:8080
inca
[lol@mysql-db01 conf]$

经过以上的配置就可以达到我们想要的降权启动nginx。但是这只是nginx优化的一个小部分。

原文地址:https://www.cnblogs.com/dadonggg/p/7798554.html