Nginx.conf配置文件详解

运行用户
user nobody;
启动进程,通常设置成和cpu的数量相等
worker_processes 1;

全局错误日志及PID文件
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;#单个后台worker process进程的最大并发链接数
}
设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
include mime.types;
default_type application/octet-stream;
#设定日志格式
#access_log logs/access.log main;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#服务器设置的最大上传文件大小
client_max_body_size 200m;
#开启gzip压缩
gzip on;
gzip_comp_level 2;
gzip_types text/css application/javascript application/json;
gzip_disable “MSIE [1-6].”;

#设定负载均衡的服务器列表
upstream collectionBackend {
#本机上的Squid开启7002端口
server 127.0.0.1:7002;
}

server {
#侦听80端口
listen 80;
#定义使用localhost访问
server_name localhost;

#默认值90s, nginx连接到后端服务器的连接超时时间
proxy_connect_timeout 90;

#设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接
proxy_send_timeout 90;

设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。
proxy_read_timeout 90;

nginx设置目录浏览及中文乱码问题解决方案:
#charset koi8-r;

#设定日志格式
#access_log logs/host.access.log main;

在这个location配置段中,如果URL请求“/public/logo.gif”,那么nginx将会在服务器上查找“F:/workspace/cloud/bv-frontend-dev/public/logo.gif”文件,即请求的URL中location后面的部分会被追加到alias指定的目录后面,而location后面的“/public”路径将会被自动丢弃。
location /public/ {
alias F:/workspace/cloud/bv-frontend-dev/public/;
}

location / {
#rewrite的主要功能是实现RUL地址的重定向
rewrite ‘^/simple/.*.(html)$’ /simple-index/simple.html last;

rewrite ‘^/simple/.*config.js$’ /simple-index/config.js last;

rewrite ‘^/simple/./custom/(.)’ /simple-index/custom/$1 last;

rewrite ‘^/simple/./api/(.)’ /api/$1 last;

rewrite ‘^/simple/custom/(.*)’ /simple-index/custom/$1 last;

rewrite ‘^/simple/api/(.*)’ /api/$1 last;

alias同上含义:
alias F:/workspace/bestvike/collection-handle/static/;
index index.html;
}

location /simple-index {
alias F:/workspace/bestvike/collection-handle/static/;
}

location /resources/ {
rewrite ‘^/resources/simple/(.*.html)$’ /simple-index/simple/$1 last;
alias F:/workspace/bestvike/collection-handle/static/simple/;
}

location /api/ {

proxy_set_header:允许重新定义或者添加发往后端服务器的请求头.该值可以包含文本、变量和它们的组合。在没有定义proxy_set_header时会继承之前定义的值。默认情况下,只有两个字段被重定义:proxy_set_header Host $proxy_host 和 proxy_set_header Connection close

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header REMOTE-HOST $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass URL,URL为要设置的被代理服务器的地址,包含传输协议、主机名称或IP地址加端口号、URI等要素。
proxy_pass http://collectionBackend/api/;
}

error_page:当发生错误的时候能够显示一个预定义的地址,实际上产生了一个内部跳转(internal redirect),当访问出现500、502、503、504的时候就能返回50x.html中的内容。同时我们也可以自己定义这种情况下的返回状态码。

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

rewrite后常用的正则表达式:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值