在本项目中使用到了Nginx进行反向代理,由于是第一次接触Nginx,也不是很熟悉,但总算现在项目能正常跑起来了,特此将nginx.cof配置文件内容记录下来,以便日后查看。
在上一篇博客中,有关于Nginx在windows下的安装、常用命令以及部分配置的讲解,有兴趣的可以看看,欢迎指错。附上博文地址:Nginx在windows下的安装、运行,以及配置文件讲解
#user nobody;
# 设置 <= cpu核数
worker_processes 1;
# 指定错误日志文件存放路径,错误日志级别可选项为【debug|info|notice|warn|error|crit】
error_log logs/warn.log warn;
# 指定pid存放路径 nginx启动后的进程ID
pid logs/nginx.pid;
# 工作模式及连接数上限
events {
# 使用网络I/O模型,Linux系统推荐使用epoll模型,FreeBSD系统推荐使用kqueue;window下不指定
# 允许的连接数
# user epoll;
worker_connections 1024;
}
# 设定http服务器,利用他的反向代理功能提供负载均衡支持
http {
# 设定mime类型
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"';
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
access_log logs/access.log main;
# 设定access log
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# 这个参数表示http连接超时时间,默认是65s。要是上传文件比较大,在规定时间内没有上传完成,就会自动断开连接!所以适当调大这个时间。
#keepalive_timeout 0;
keepalive_timeout 5000;
# 开启gzip模块
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain application/x-javascript text/css application/xml;
output_buffers 1 32k;
postpone_output 1460;
server_names_hash_bucket_size 128;
client_header_timeout 3m; #调大点
client_body_timeout 3m; #调大点
client_max_body_size 200m; #主要是这个参数,限制了上传文件大大小
#client_body_buffer_size 1024;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_vary on;
# 设定负载均衡的服务器列表
upstream location {
# 同一机器在多网情况下,路由切换,ip可能不同 #weigth参数表示权值,权值越高被分配到的几率越大
server 222.17.30.11:8081;
server 222.17.30.12:8082;
server 222.17.30.13:8083 backup;
}
#设定虚拟主机
server {
listen 9876;
server_name location;
# 设置url编码格式,解决参数中文乱码问题
charset UTF-8;
# 设定本虚拟主机的访问日志
access_log logs/host.access.log main;
# 对静态资源进行映射
location ^~ /staticFile/ {
alias E:/Zxztb/staticFile/;
}
# 对 "/" 启用负载均衡
location / {
# 设定代理访问地址
proxy_pass http://location/;
proxy_redirect default;
# 解决ajax跨域问题
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
# 保留用户真实信息
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 允许客户端请求的最大单个文件字节数
client_max_body_size 100m;
# 缓冲区代理缓冲用户端请求的最大字节数,可以理解为先保存到本地再传给用户
client_body_buffer_size 1280k;
# nginx跟后端服务器连接超时时间,发起握手等候响应超时时间(代理连接超时)
proxy_connect_timeout 5;
# 连接成功后 等待后端服务器响应时间 其实已进入后端的排队之中等候处理
proxy_read_timeout 60;
# 代理请求缓存区 这个缓存区间会保存用户的头信息一共Nginx进行规则处理 一般只要能保存下头信息即可
proxy_send_timeout 30;
# 同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
proxy_buffer_size 256k;
proxy_buffers 4 256k;
# 如果系统很忙的时候可以申请国内各大的proxy_buffers 官方推荐 *2
proxy_busy_buffers_size 256k;
# proxy 缓存临时文件的大小
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}