nginx配置文件说明(四)

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等

发布了115 篇原创文章 · 获赞 58 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/luChenH/article/details/104770409