1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 【Nginx】Nginx配置文件参数/启动参数详解;启动/停止/重新加载配置命令

【Nginx】Nginx配置文件参数/启动参数详解;启动/停止/重新加载配置命令

时间:2023-04-22 09:49:47

相关推荐

【Nginx】Nginx配置文件参数/启动参数详解;启动/停止/重新加载配置命令

nginx配置文件

nginx及其模块的工作方式是由配置文件指定,默认情况下配置文件被命名为nginx.conf并且存放在/usr/local/nginx/conf或者/etc/nginx或者/usr/local/etc/nginx

默认的config

#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_nopushon;#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_certificatecert.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;# }#}}

nginx文件结构

... #全局块events {#events块...}http#http块{... #http全局块server #server块{... #server全局块location [PATTERN] #location块{...}location [PATTERN] {...}}server{...}...#http全局块}

含义

1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

5、location块:配置请求的路由,以及各种页面的处理情况。

配置文件示例

########### 每个指令必须有分号结束。##################user administrator administrators; #配置用户或者组,默认为nobody nobody。#worker_processes 2; #允许生成的进程数,默认为1#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emergevents {accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为onmulti_accept on; #设置一个进程是否同时接受多个网络连接,默认为off#use epoll;#事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventportworker_connections 1024; #最大连接数,默认为512}http {include mime.types; #文件扩展名与文件类型映射表default_type application/octet-stream; #默认文件类型,默认为text/plain#access_log off; #取消服务日志 log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式access_log log/access.log myFormat; #combined为日志格式的默认值sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。upstream mysvr {server 127.0.0.1:7878;server 192.168.10.121:3333 backup; #热备}error_page 404 ; #错误页server {keepalive_requests 120; #单连接请求上限次数。listen 4545; #监听端口server_name 127.0.0.1; #监听地址 location ~*^.+$ {#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。#root path; #根目录#index vv.txt; #设置默认页proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表deny 127.0.0.1; #拒绝的ipallow 172.18.5.54; #允许的ip } }}

上面是nginx的基本配置,需要注意的有以下几点:

1、1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 2.$remote_user :用来记录客户端用户名称; 3.$time_local : 用来记录访问时间与时区;4.$request : 用来记录请求的url与http协议;5.$status : 用来记录请求状态;成功是200, 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;7.$http_referer :用来记录从那个页面链接访问过来的; 8.$http_user_agent :记录客户端浏览器的相关信息;2、惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。3、每个指令必须有分号结束。

nginx命令

要启动nginx直接运行nginx文件,启动后可以使用以下命令。

nginx -s [options]

例如,/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf(带配置文件参数的启动nginx)

options参数可以是下面之一:

stop- 快速关机quit- 优雅的关机 (等待工作进程完成当前请求的服务时,停止nginx进程)reload- 重新加载配置文件 (当nginx配置发生更改的时候,需要指定该命令才会生效。一旦主进程收到该命令,它将首先检查配置文件的语法正确性然后尝试应用新的配置,如果应用成功,主进程将会启动新的工作进程的同时向旧工作进程发送关闭请求,否则的话主进程将回滚更改继续使用旧的配置。当旧进程接收关闭命令,旧进程会停止接收新的请求同时完成正在处理的请求,最后旧工作进程退出)reopen- 重新打开日志文件

查看nginx进程的列表

ps -ax

该命令可以看到所有进程包括进程ID,默认情况下主进程的ID将写入nginx.pid目录,

如果需要正常关闭某个进程,执行命令:kill -s QUIT 进程ID

参数详解

sudo nginx #启动 nginxnginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出nginx -t #测试配置文件nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]-?,-h : 打开帮助信息-v: 显示版本信息并退出-V: 显示版本和配置选项信息,然后退出-t: 检测配置文件是否有语法错误,然后退出-q: 在检测配置文件期间屏蔽非错误信息-s signal : 给一个 nginx 主进程发送信号:stop(停止), quit(退出), reopen(重启), reload(重新加载配置文件)-p prefix : 设置前缀路径(默认是:/usr/local/Cellar/nginx/1.2.6/)-c filename: 设置配置文件(默认是:/usr/local/etc/nginx/nginx.conf)-g directives : 设置配置文件外的全局指令

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。