Nginx config.conf 配置 负载动静分离及拦截指定url请求

nginx config.conf  server 负载均衡、动静分离、https证书配置demo.
工作实战笔记

nginx.conf 设置及性能调优

user  nobody;
#这里一般跟你机器的cpu核数相同性能最好
worker_processes  2; 
#这个配置是当连接数过大以后linux对操作文件数量的限制
worker_rlimit_nofile 65535;
# 这个是请求异常日志,根据情况看是否打开
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  /home/nginx/log/error.log  error;

#指定work线程连接数量,根据上线实际情况可以调整大小
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
  #指定日志打印格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
			'$status $body_bytes_sent $request_body "$http_referer" '
			'"$http_user_agent" "$http_x_forwarded_for" "$request_time" "$upstream_response_time"';

	#这个设置是每次请求的记录日志,根据情况看是否需要打开
    #access_log  logs/access.log  main;
	access_log off;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  30;
    send_timeout 10;
    tcp_nodelay on;

    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  on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_comp_level 3;

    gzip_types text/plain text/css text/xml text/javascript image/gif image/jpeg image/png application/x-javascript application/xml;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    gzip_http_version 1.1;
    gzip_proxied      any;
    gzip_vary on;
    #服务器配置的server 我都放到了外边,因为nginx代理了好多
    #域名 方便操作一个域名做了一个配置文件
    include /usr/local/nginx/conf/vhost/*.conf;

}

server动静分离配置

	server {
		#监听的端口
        listen 80;
        #监听的域名
        server_name tongji.xxx.com 192.168.9.5;
        index  index.html index.htm;
        #指定静态资源放置的目录
        root   /home/www/tongji.xxx.com;
		#拦截静态资源去走 静态资源目录
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires   5m;
        }
        location ~ .*\.(js|css)?$ {
            expires      5m;
        }
		#拦截接口请求转发到tomcat服务器
        location ~ .*\.(jsp|do|action)?$ {
        #下边这几个参数是将请求的客户端信息放入header中
        proxy_set_header Host  $host;
		proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        # proxy_set_header X-Forwarded-For  $remote_addr;
		proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for; 
		#tomcat服务器地址
                proxy_pass http://127.0.0.1:8082;
        }
		#请求日志记录的地址
        access_log /home/nginx/log/xxx.log ;
    }

server https配置 动静分离 拦截指定请求

server {
	#https走的是443端口 这里一定要注意
    listen       443 ssl;
    server_name  www.xxx.com;

        index  index.html index.htm;
        root   /data/www/www.xxx.com.chart;
	#这里配置证书路径
    ssl_certificate      /etc/nginx/cert/2769182_weixincard.com.pem;
    ssl_certificate_key  /etc/nginx/cert/2769182_weixincard.com.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;
	#这里是拦截域名过来后 后边跟着 /jingluobi的请求
    location ~ /jingluobi.*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires   5m;
        }
    location ~ /jingluobi.*\.(js|css)?$
        {
                expires      5m;
        }
#这里是拦截域名过来后 后边跟着 /jingluobi的请求
    location ~ /jingluobi.*\.(jsp|do|action)?$ {
                        proxy_set_header Host  $host;
                        proxy_set_header X-Forwarded-For  $remote_addr;
                        proxy_pass http://127.0.0.1:8081;
        }

        access_log /data/www/nginx/log/chart.log ;
}

##nginx 负载均衡配置demo

	#这是要负载的机器
    upstream upstream_name{
        server 192.168.0.28:8001;
        server 192.168.0.28:8002;
    }

    server {
        listen       8080;
        server_name  localhost;
		#拦截到请求以后让他请求 需要负载的upstream集群
		#当然这里也可以配置的跟上边一样负载连接特定请求等等
        location / {
            proxy_pass http://upstream_name;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

你可能感兴趣的:(nginx)