一、nginx.conf 设置结构
函数
| 说明
| main
| 全局设置
| event
| 设置工作模式以及连接数
| http
| http模块相干设置
| server
| 捏造主机设置,可以有多个
| location
| 路由规则,表达式
| upstream
| 集群、内网服务器(负载均衡也在这里边配)
|
二、Nginx设置语法
基本的语法:
指令集组成:每个指令单独写一行,每个指令分号 ";" 分开,每个指令块用大括号 "{ ... }" 分开,大括号的后方没有分号。注释用#号分开。
$符号:$符号为nginx内部提供的一些参数变量。
三、nginx.conf 核心设置文件详解
函数
| 说明
| main
| 全局设置
| event
| 设置工作模式以及连接数
| http
| http模块相干设置
| server
| 捏造主机设置,可以有多个
| location
| 路由规则,表达式
| upstream
| 集群、内网服务器(负载均衡也在这里边配)
|
主设置文件详解
(一)main 全局设置模块
1、历程用户设置
- user root;
- worker_processes 10;
- worker_rlimit_nofile 65535;
复制代码
- user root;
- 这一设置项指定了 Nginx 工作历程所利用的用户身份。root 是体系中的超等用户,拥有最高权限。不过,从安全角度思量,不建议让 Nginx 以 root 用户身份运行,因为这会使 Nginx 拥有过高的权限,一旦出现安全毛病,攻击者大概会获取体系的最高控制权。通常,建议创建一个专门的低权限用户来运行 Nginx。
- worker_processes 4;
- 此设置项用于设置 Nginx 工作历程的数量。Nginx 采取多历程模子,一个主历程(master process)负责管理多个工作历程(worker processes),工作历程负责处理处罚实际的客户端请求。4 代表创建 4 个工作历程。一样平常而言,可以根据服务器的 CPU 核心数来设置该值,通常设置为 CPU 核心数或者核心数的两倍,如许能充分利用服务器的 CPU 资源。
- worker_rlimit_nofile 65535;
- 该设置项设定了每个 Nginx 工作历程能够打开的最大文件描述符数量。在 Linux 体系里,一切皆文件,包括网络连接、磁盘文件等,每个打开的文件或者连接都会占用一个文件描述符。65535 意味着每个工作历程最多可以同时打开 65535 个文件描述符。当服务器必要处理处罚大量并发连接时,就必要增大这个值,防止出现 “too many open files” 的错误。
2、 nginx日志路径设置
- #error_log logs/error.log; #错误的日志,在编译的时候已经设置相关的路径放:/var/log/nginx/
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
复制代码
3、存放pid的地方
- #pid logs/nginx.pid; #进程号存在的路径,在编译的时候已经设置相关的路径放:/var/run/nginx/
复制代码
(二)、events设置工作模式以及连接数
- events {
- #默认使用epoll
- use epoll;
- #每个worker允许客端连接的最大连接数,根据硬件的配置来选值的大小。
- worker_connections 1024;
- }
复制代码
(三)、http相干网络传输的模块(包罗了许多的设置内容)
mime.types文件

3.1、日志格式
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #access_log 日志的格式,可以自定义格式。
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #access_log logs/access.log main;
- ***参数注解区****
- # $remote_addr 客户端的IP地址
- # $remote_user 用户名称,可以是 "-"
- # [$time_local] 访问时间
- # $request 请求的内容包括:URL 请求的方法GET、POST
- # $status 响应的状态码
- # $body_bytes_sent 客户端发送的文件主体所包含内容的字节数
- # $http_referer 记录着用户从哪个访问链接跳转过来的,我们在做日志分析的时候会用到。
- # $http_user_agent 用户的代理
- # $http_x_forwarded_for 可以记录客户端的IP
复制代码
3.2、文件的高效传输
- sendfile on; #打开,表示文件传输的性能会得到提升,nginx的性能也得到相应的提升。
- #tcp_nopush on; #和sendfile一起使用,表示当我们的数据包累积到一定的大小之后再发送,可以提高传输的效率。先取数据在进行统一分发。
复制代码
3.3、客户端连接服务器的超时时间(传输完成后保持的时间)
- keepalive_timeout 65; #以秒为单位,http有keepalive机制,当数据传输完成之后会保持一定时间的连接处于打开状态,如果客户端有新的请求会用此连接去处理。不用创建新的连接,节省资源的开销。
复制代码
3.4、gzip压缩
- #gzip on; #内容的传输经过压缩之后体积变小,提升的传输速率,减少了带宽的产生,但是在压缩的过程中会消耗我们系统上CPU的性能。
复制代码
3.5、server模块,捏造主机相干设置
- server {
- listen 8080; #服务端口号
- server_name localhost; #服务IP、域名
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / { #配置页面显示的路由: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;
- # }
- #}
- }
复制代码
3.5.1 在nginx.conf文件中添加新的server模块。
- server {
- listen 8888; #指定的服务端口为8888
- server_name 127.0.0.1; #指定的服务器的名称是127.0.0.1
- location / {
- root html;
- index test.html index.htm; #访问到的内容为test.html文件
- }
复制代码
3.5.2 添加test.html文件:/usr/local/nginx/html/test.html
3.6、通过include函数的调用server模块的设置,进步文件的可读性。
3.6.1 在nginx.conf文件中定义include调用server模块(支持正则匹配)
可用统一将设置文件放在/usr/local/nginx/conf/conf.d指定的路径下如许方便管理,如:
- HTTP相干的设置放在:/usr/local/nginx/conf/conf.d/http 目录下
- TCP相干的设置放在:/usr/local/nginx/conf/conf.d/tcp 目录下
- user root;
- worker_processes 4;
- worker_rlimit_nofile 65535;
- events {
- ...
- }
- include conf.d/tcp/*.conf; #TCP相关配置(不能放在下边的HTTP模块中不然会报错);这里的include是指包含/usr/local/nginx/conf/conf.d/tcp路径下所有的.conf。
- http {
- ...
- include conf.d/http/*.conf; #HTTP相关配置(需要放在HTTP模块中不然会报错);这里的include是指包含/usr/local/nginx/conf/conf.d/http 路径下所有的.conf
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |