Nginx生产配置大全-反向代理与负载均衡

1、Nginx安装

下载地址 http://nginx.org/en/download.html
依赖安装开发工具包:
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

解压安装包进入nginx源码目录,配置环境变量
./configure --prefix=/usr/local/nginx --with-pcre
–with-http_stub_status_module --with-http_ssl_module
–with-http_gzip_static_module --with-http_realip_module \

编译并安装nginx源码
make && make install

2、编译说明

–prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx

–conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf

–user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。

–with-pcre : 设置PCRE库的源码路径,使用–with-pcre自动找到库文件。perl正则表达式使用在location指令ngx_http_rewrite_module模块中。

–with-zlib=PATH : 指定zlib的源码解压目录。启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。

–with-http_ssl_module : 使用https协议模块,前提是openssl与openssl-devel已安装

–with-http_stub_status_module : 用来监控 Nginx 的当前状态

–with-http_realip_module : 改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),能够使得后台服务器记录原始客户端的IP地址。

–add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。

2、Nginx配置

user root root;	#用户和用户组
worker_processes 8; #nginx进程数,设置为等于CPU总核心数。
worker_rlimit_nofile 65535; #指定进程可以打开的最大文件数

#全局错误日志
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;	#进程pid文件


events {
    
    worker_connections  2048;	#单个进程最大连接数
    accept_mutex on;	#设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  	#设置一个进程是否同时接受多个网络连接,默认为off 
    
}


http {
    include       mime.types;	#文件扩展名与文件类型映射表
    default_type  application/octet-stream;
    server_tokens   off; # 关闭nginx版本号
    #输出日志格式
    log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
  
    charset UTF-8;

    #access_log  logs/access.log  main;

    sendfile        on;	#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。
    #tcp_nopush     on;

    keepalive_timeout 180;	#长连接超时时间,单位是秒
    
    #FastCGI优化服务性能,提高访问速度。
    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模块设置
    gzip on; #开启gzip压缩输出
    gzip_min_length 5k;    #最小压缩文件大小
    gzip_buffers 4 16k;    #压缩缓冲区
    gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 2;     #压缩等级
    gzip_types text/plain application/x-javascript text/css application/xml;#压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;

    #负载均衡配置的4中方式
    upstream myserver {
     
        #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的机率越大。
        server 192.168.172.51:80 weight=1;
        server 192.168.172.52:80 weight=3;

        #nginx的upstream目前支持4种方式的分配
        #1、轮询(默认)
        #每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
        #2、权重
        #指定轮询机率,weight和访问比率成正比,用于后端服务器性能不均的情况。
        #例如:
        #upstream myserver {
        #    server 192.168.0.54 weight=2;
        #    server 192.168.0.55 weight=3;
        #}
        #2、ip_hash
        #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
        #例如:
        #upstream myserver {
        #    ip_hash;
        #    server 192.168.0.54:80;
        #    server 192.168.0.55:80;
        #}
        #3、fair(第三方)
        #按后端服务器的响应时间来分配请求,响应时间短的优先分配。
        #upstream myserver {
        #    server server1;
        #    server server2;
        #    fair;
        #}
        #4、url_hash(第三方)
        #按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
        #例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
        #upstream myserver {
        #    server squid1:9527;
        #    server squid2:9527;
        #    hash $request_uri;
        #    hash_method crc32;
        #}

        #tips:
        #upstream bakend{#定义负载均衡设备的Ip及设备状态}{
        #    ip_hash;
        #    server 192.168.0.1:9090 down;
        #    server 192.168.0.1:9080 weight=2;
        #    server 192.168.0.1:9060;
        #    server 192.168.0.1:9070 backup;
        #}
        #在需要使用负载均衡的server中增加 proxy_pass http://myserver/;

        #每个设备的状态设置为:
        #1.down表示单前的server暂时不参与负载
        #2.weight为weight越大,负载的权重就越大。
        #3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
        #4.fail_timeout:max_fails次失败后,暂停的时间。
        #5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

        #nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
        #client_body_in_file_only设置为On
        #client_body_temp_path设置记录文件的目录 可以设置最多3层目录
        #location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
    }
     
     
    server {
        listen       80;
        server_name  xxx.com;

        charset UTF-8;
        access_log  logs/80_access.log;
        error_log  logs/80_error.log;
	 
        location / {
	    #root path;  					#根目录
          #index index.html;  			#设置默认页面
	    proxy_pass http://myserver;		#反向代理到upstream上游服务器
          proxy_http_version 1.1; 			#长连接配置支持HTTP1.1协议
	    proxy_set_header Upgrade $http_upgrade;
	    proxy_set_header Connection "upgrade";
	    proxy_set_header Host $host;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header X-Forwarded-Proto $scheme;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    client_max_body_size       1024k;	#客户端请求的最大单文件字节数
	    client_body_buffer_size    1024k;	#客户端缓冲区代理缓冲用户端请求的最大字节数
	    proxy_connect_timeout      180;		#nginx跟后端服务器连接超时时间
	    proxy_send_timeout         180;		#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
	    proxy_read_timeout         180;		#连接成功后,后端服务器响应时间
	    proxy_buffer_size          512k;	#代理服务器读取的第一部分应答的缓冲区大小
	    proxy_buffers              4 512k; 	#缓冲区大小
	    proxy_busy_buffers_size    512k;	#高负荷下缓冲大小(proxy_buffers*2)
	    proxy_temp_file_write_size 1024k;	#设定缓存文件大小
        }

	#静态文件,nginx自己处理,不去请求后端tomcat
	#正则语法说明:* ~ 为区分大小写匹配,* ~* 为不区分大小写匹配,* !~!~*分别为区分大小写不匹配及不区分大小写不匹配
	#location ~* /download/ {
	#	root /app;
	#}
	
	#location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
	#{
	#	root /app;  #静态资源在nginx服务器上的目录
	#	expires 30d; #静态资源缓存时间30天
	#}
	
	#location /status {
	#	stub_status on;
	#	access_log off;
	#	allow 192.168.80.53/54; #允许设置的IP访问,设置白名单
	#	deny all;		# 拒绝白名单以外的IP访问
	#}
	  
	#location ~ ^/(WEB-INF)/ {
	#	deny all;	# 拒绝白名单以外的IP访问
	#}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
	  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  xxx.com;

    #last 相当于Apache里的[L]标记,表示完成rewrite;* break 终止匹配, 不再匹配后面的规则;* redirect 返回302临时重定向 地址栏会显示跳转后的地址;* permanent 返回301永久重定向 地址栏会显示跳转后的地址
    
    #反向代理重定向
    #	if ( $request_uri = "/" ) {
    #         rewrite "/" https://$server_name$request_uri; break;
    #        }

	#if ($scheme = http) {
	#return 301 https://$server_name$request_uri;
	#}

	#if ($server_port = 80) {
	#return 301 https://$server_name$request_uri;
	#}

	
    #error_page 497 https://$server_name$request_uri;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3、Nginx启停

#检查配置文件是否正确
/usr/local/nginx/sbin/nginx -t

#查看Nginx信息
/usr/local/nginx/sbin/nginx -V

#关闭
/usr/local/nginx/sbin/nginx -s stop 或 pkill nginx

#启动
/usr/local/nginx/sbin/nginx

#重启
/usr/local/nginx/sbin/nginx -s reload

作者简介
思维的持续,一个真的有思想,不穿格子衬衫的程序员。
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章