话不多说直接上配置!
worker_processes 1;#线程打开2个,有一个是主线程,运行很稳定
events {
worker_connections 1024;#工作进程的最大连接数量
}
#反向代理设置
upstream server_list{
server localhost:8081;
server localhost:8082;
server localhost:8083;
}
http {
include mime.types;#纳入 mime.types 文件的配置
default_type application/octet-stream;#定义文件类型
sendfile on;#开启高效文件传输模式
keepalive_timeout 65;#指定客户端与服务端建立连接后发送 request body 的超时时间
server {
listen 80;#端口
server_name localhost;#IP名称
client_max_body_size 100M;#限制请求体的大小,若超过所设定的大小,返回413错误
proxy_max_temp_file_size 1000M;#限制临时缓存大小
proxy_connect_timeout 60;#http请求无法立即被容器(tomcat, netty等)处理,被放在nginx的待处理池中等待被处理。此参数为等待的最长时间,默认为60秒,官方推荐最长不要超过75秒
proxy_send_timeout 60;#http请求被服务器处理完后,把数据传返回给Nginx的用时,默认60秒
proxy_read_timeout 60;#http请求被容器(tomcat, netty等)处理后,nginx会等待处理结果,也就是容器返回的response。此参数即为服务器响应时间,默认60秒
send_timeout 60;#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据 nginx使用proxy模块时,默认的读取超时时间是60s
#默认访问页面
location / {
root html;
index index.html index.htm;
}
#访问接口api的地址 进行反向代理和负载均衡
location /api/ {
proxy_pass http://server_list;#代理到上面某个的服务中,默认轮询
}
#以下解决跨域问题
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#指定出现500、500、502、503、504等错误跳转的页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}