1.0首先看一下,nginx安裝後默認的配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
#user nobody;
#代表以nobody用戶啓動,一般我們會修改爲 user nginx nginx; ====>代表以nginx 用戶組nginx用戶啓動
worker_processes 1;
#代表worker 進程的個數, 生產環境一般設置爲與CPU核數相同。如四核CPU則設置爲4
worker_connections 1024;
查看linux可處理的最大文件數
ulimit -a|grep "open files"
說明系統可同時打開的最大文件數爲65535。我們設置的時候要小於等於這個數。
#nginx的最大連接數,默認處理同時處理1024個請求。 設置爲65535個(算法:靜態資源服務器是65535個)。 若web服務器設置爲32767(反向代理時,相當於兩個鏈接一個連接客戶端,一個連接serve。則個數爲 65535/2個)
查看nginx最大可以打開的文件數
sysctl -a |grep file
思想,linux,一切皆文件。那麼一個鏈接也是一個文件。那麼滿足我們的設置 。65535大於我們所設置的32767個數。
系統當前默認打開的文件數
ulimit -a
若此個數,滿足我們nginx的需求。 65535大於我們所設置的32767個數。
可以通過配置 vim /etc/security/limits.conf 最後一行添加
查看當前用戶nginx
id nginx
發現沒有當前用戶。
添加當前用戶
運行服務的用戶我們都會設置nologin。不讓登陸系統
useradd -s /sbin/nologin nginx
然後啓動nginx
查看運行
ps aux
此時發現當前用戶啓動一個worker,這是我們設置的worker數,並且啓動的是以nginx用戶啓動的。那麼這就方便了我們的管理
2.0製作腳本服務
不自己編寫,我們複製一份進行改造
cd /etc/systemd/system
vi 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
# Start main service
ExecStart=/usr/local/nginx/sbin/nginx
ExceReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
則可支持systemctl start nginx.service
systemctl stop nginx.service等