nginx.conf
# 设置nginx服务的系统使用用户
user nginx;
# 工作进程数
worker_processes auto;
# 错误日志目录
error_log /var/log/nginx/error.log;
# nginx服务启动时候pid
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# 加载模块
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;//每个进程允许最大连接数/工作进程数
}
# HTTP请求
http {
#日志格式
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 /var/log/nginx/access.log main;
# 底层设置
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# 加载配置
include /etc/nginx/conf.d/*.conf;
# 配置站点
server {
# 监听80端口
listen 80 default_server;
listen [::]:80 default_server;
# 域名
server_name _;
# 站点目录
root /usr/share/nginx/html;
# Load configuration files for the default server block.
# 加载站点配置
include /etc/nginx/default.d/*.conf;
# 下面根据url定义方法
location / {
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
worker_processes
设置成CPU数量最好,简单说就是nginx会开一个主进程,然后开始招募,CPU数量就是工人数量,高了招不到,低了有工人浪费。现在都是auto自动了。
log_format
设置日志格式,帮助分析服务器状态,代码效率。 官网文档:http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
$arg_name 请求中的参数名,比如请求http://localhost?a=x&b=y, 则 $arg_a 表示的就是字符串’x’
$args 请求中的参数值
$binary_remote_addr 客户端地址的二进制形式, 固定长度为4个字节
$body_bytes_sent 传输给客户端的字节数,响应头不计算在内
$bytes_sent 传输给客户端的字节数
$content_length “Content-Length” 请求头字段
$remote_addr 客户端地址
$remote_user 用于 HTTP 基础认证服务的用户名
$request_body 客户端的请求主体
$request_length 请求的长度 (包括请求的地址, http请求头和请求主体)
$request_method HTTP 请求方法
$request_time 处理客户端请求使用的时间,从读取客户端的第一个字节开始计时
$request_uri 这个变量等于包含一些客户端请求参数的原始 URI ,它不包含主机名
$server_addr 服务器端地址, 注意:为了避免访问 linux 系统内核,应将ip地址提前设置在配置文件中
$status HTTP 响应代码
$time_local 服务器时间
$uri 请求中的当前 URI, 不带请求参数,且不包含主机名
listen
这个设置指定监听,比如内部通讯。
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;
简单的反向代理
server {
listen 9000;
location /web1 {
proxy_pass http://localhost:8081/web/web1.html;
}
location /web2 {
proxy_pass http://localhost:8081/web/web2.html;
}
}
其他指令:
proxy_set_header:在将客户端请求发送给后端服务器之前,更改来自客户端的请求头信息;proxy_connect_timeout:配置 Nginx 与后端代理服务器尝试建立连接的超时时间;proxy_read_timeout:配置 Nginx 向后端服务器组发出 read 请求后,等待相应的超时时间;proxy_send_timeout:配置 Nginx 向后端服务器组发出 write 请求后,等待相应的超时时间;proxy_redirect:用于修改后端服务器返回的响应头中的 Location 和 Refresh。
stream模块
nginx不禁可以监听http,也可以tcp/ip。
stream {
server {
listen 3000;
return '3000 server get ip: $remote_addr!\n';
}
}
location
server 块可以包含多个 location 块,location 指令用于匹配 uri,语法:
location [ = | ~ | ~* | ^~] uri {
...
}
匹配规则
= 严格匹配。如果请求匹配这个 location,那么将停止搜索并立即处理此请求
~ 区分大小写匹配(可用正则表达式)
~* 不区分大小写匹配(可用正则表达式)
!~ 区分大小写不匹配
!~* 不区分大小写不匹配
^~ 前缀匹配
@ “@” 定义一个命名的location,使用在内部定向时
/ 通用匹配,任何请求都会匹配到
匹配顺序
“=” 精准匹配,如果匹配成功,则停止其他匹配。 普通字符串指令匹配,优先级是从长到短(匹配字符越多,则选择该匹配结果)。匹配成功的location如果使用^~,则停止其他匹配(正则匹配)。 正则表达式指令匹配,按照配置文件里的顺序(从上到下),成功就停止其他匹配。 如果正则匹配成功,使用该结果;否则使用普通字符串匹配结果。
(location =) > (location 完整路径) > (location ^~ 路径) > (location ,* 正则顺序) (location 部分起始路径) > (location /)
(精确匹配)> (最长字符串匹配,但完全匹配) >(非正则匹配)>(正则匹配)>(最长字符串匹配,不完全匹配)>(location通配)
开启跨域
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,Keep-Alive,User-Agent,Cache-Control,Content-Type,Authorization';
if ($request_method = 'OPTIONS') {
return 204;
}
}