給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優化的一個小部分。
