文章目录
- 1. nginx配置文件位置(ubuntu环境)
- 2. 备份配置文件
- 3. 配置文件组成
- 3.1 全局块
- 3. 2 events块
- 3.3 http块
- 3.3.1 http全局块
- 3.3.2 server块
- 3.3.2.1 全局server块
- 3.3.2.1 location块
1. nginx配置文件位置(ubuntu环境)
/usr/local/nginx/conf
2. 备份配置文件
养成一个好的习惯,在修改配置文件的时候,要备份配置文件。
root@:/usr/local/nginx/conf# cp nginx.conf nginx.conf.back
3. 配置文件组成
3.1 全局块
配置文件开始到events块之间的内容。主要会设置一些影响nginx服务器整体运行的配置指令,主要包括配置运行nginx服务器的用户(组),允许生成的work_process数,进程PID存放路径,日志存放路径和类似以及配置文件的引入等。
#user nobody;
# nginx服务器并发处理服务的关键配置
# worker_processes值越大,可以支持的并发处理量也越多,但是会受到硬件和软件等设备的制约。
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
3. 2 events块
events块涉及的指令主要影响nginx服务器与用户的网络连接,常用的设置包括是否开启对多work_process下的网络连接进行序列化,是否允许同时接受多个网络连接,选取哪种事件驱动模型来处理连接请求,每个work_precess可以同时支持的最大连接数等。
events {
# 1024最大连接数
worker_connections 1024;
}
3.3 http块
这是nginx服务器配置中最频繁的部分,代理,缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。需要注意的是:http块包含http全局块、server块。
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 8010;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://localhost:8080;
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;
}
}
3.3.1 http全局块
http全局块配置的指令包括文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限等。
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;
3.3.2 server块
每个http包含多个server块,而每个server块就相当于一个虚拟机。
而每个server块也分为全局server块,以及同时包含多个location块。
server {
listen 8010;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://localhost:8080;
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;
}
}
3.3.2.1 全局server块
主机的监听配置和主机的名称或ip配置
listen 8010;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
3.3.2.1 location块
一个server块可以配置多个location块。
这块的主要作用是基于nginx服务器接收到的请求字符串(例如:server_name/uri-string),对虚拟主机名称(也可以是ip别名)之外的字符串(例如:前面的/uri-string)进行匹配,对特定的请求进行处理,地址定向,数据缓存和应答控制等功能,还用许多第三方模块的配置也在这里进行。
location / {
root html;
proxy_pass http://localhost:8080;
index index.html index.htm;
}