nginx配置

177 阅读3分钟

配置nginx.conf

#运行用户
user  root;
#启动的进程数,与CPU数量设置相同
worker_processes  2;
worker_rlimit_nofile 65535; 
#错误日志
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#PID文件
pid        logs/nginx.pid;

#事件模型
events {
	#默认是on。设置为on后,多个worker按串行方式来处理连接;为off时并行处理连接
	multi_accept on; 
	#参考事件模型,kqueue | rtsig | epoll | /dev/poll | select | poll  
    #epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型
    #如果跑在FreeBSD上面,就用kqueue模型。  
	use epoll; 
	#单个进程最大连接数(最大连接数=连接数*进程数) 
	worker_connections 65535; 
}


http {
	#文件扩展名与文件类型映射表  
    include       mime.types;
	 #默认文件类型  
    default_type  application/octet-stream;
	#默认编码  
	#charset utf-8; 
	#服务器名字的hash表大小  
    server_names_hash_bucket_size 256; 
	#最大客户端头信息缓冲区大小
    large_client_header_buffers 4 256k;
    fastcgi_buffers 32 8k;
	# 客户端头信息缓冲区大小
	client_header_buffer_size 256k; 
	# 允许客户端请求的最大单文件字节数,上传的文件
    client_max_body_size           128m;
	# 缓冲区代理缓冲用户端请求的最大字节数
    client_body_buffer_size        1024k;
	#客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request header,
	#Nginx 返回 HTTP 408(Request Timed Out)默认值是60s。	
    client_header_timeout          30s;
	#指定客户端与服务端建立连接后发送 request body 的超时时间。如果客户端在指定时间内没有发送任何内容,
	#Nginx 返回 HTTP 408(Request Timed Out),默认值是60s。
    client_body_timeout            30s;
	#服务端向客户端传输数据的超时时间,默认值是60s。
    send_timeout                   30s;
	#临时文件最大大小,在大文件的环境下,如果想启用临时缓存,那么可以修改配置,值改成你想要的默认值是1024M,如果设置为0,标识不适用临时缓存,即直接中转发送给客户端。
	proxy_max_temp_file_size       2048m;
	#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,
	#如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。  
    sendfile        on;
    #将tcp_nopush和tcp_nodely两个指令设置为on,用于防止网络阻塞。
    tcp_nopush      on;
	#keepalive 长连接超时时间,单位是秒,0代表禁止,Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。
    keepalive_timeout  120;
	#将tcp_nopush和tcp_nodely两个指令设置为on,用于防止网络阻塞。
    tcp_nodelay on;
	#为错误页面上的nginx版本信息,建议关闭,提升安全性
	server_tokens  off;    
	#跨域
    #add_header Access-Control-Allow-Origin *;
    #add_header Access-Control-Allow-Headers X-Requested-With;
    #add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
	# 是否开启gzip压缩
    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page  404              /404.html;
    }


#配置转发
include vhosts/*.conf;
}

vhosts/default.conf

#反向代理服务配置
upstream   access_api {
      server 192.192.192.113:8101 max_fails=1 fail_timeout=30s;

}
#websoceket 使用map
map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}

server {
        listen 7001;   #协议访问端口号
        server_name 192.192.192.113; #域名
        root html;
        index index.html index.htm;
		location / {
            root   /usr/share/nginx/html/shianxinweb;
            index  index.html index.htm;
        }
        location /api {
            proxy_pass http://access_api;
            proxy_set_header Host      $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_connect_timeout 3600; 
            proxy_read_timeout 3600;
            proxy_send_timeout 3600;

        }
	location /images {
            alias   /usr/local/nginx/html/images/;
            autoindex on;	
        }
}