Nginx 如何配置:反向代理、负载均衡、动静分离功能

Nginx 配置文件在安装目录下的 conf 目录 ,即 nginx 安装目录/conf/nginx.conf

1、默认配置文件

#user  nobody;
worker_processes  1;	# 工作进程数,最好与 CPU 核数相同

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


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 "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #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;

        # 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  localhost;

    #    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;
    #    }
    #}

}

2、去掉注释后的默认配置文件

配置文件中带 # 的行是被注释的,不是必须的,下面将去掉注释:

worker_processes  1;	# 工作进程数,最好与 CPU 核数相同

events {
    worker_connections  1024;	# 每个进程可以接受多少个连接
}

# 核心
http {
    include       mime.types;				# 引入 http mime 类型,表示文件是什么类型,mime.types 是 conf 目录下的一个文件
    default_type  application/octet-stream;	# 如果 mime 类型没匹配上,默认使用二进制流的方式传输

    sendfile        on;						# 使用 linux 的 sendfile(socket,file,len) 高效网络传输,也就是零拷贝

    # 当进行TCP连接后,如果在 65s 内没有活动,则关闭长连接
    # 即在点击一个链接后,在65s内没有点击另一个链接,则会关闭当前TCP连接
    # 如果在65s内点击了其它链接,则会复用当前的TCP连接,不用进行三次握手
    keepalive_timeout  65; 
    #keepalive_timeout  0;	# 超时时间为 0 表示不使用 TCP 长连接
	
	# 一个主机,一个 server 表示一个主机,根据端口号来区别
    server {
        listen       80;					# 监听的端口号
        server_name  localhost;				# 主机名,可以配置域名或主机名,可以配置多个,用 ',' 隔开  
		
		# http://www.baidu.com/xxx/xxx.html
		# |协议|     域名     |   资源路径 uri
		# 协议+域名是 URL
		# location 是匹配 uri 的
        location / {
            root   html;		# root 的值是相对路径,相对于 nginx 安装目录,即在安装目录下的 html 目录
            index  index.html index.htm;	# 默认页,如果请求路径中没有指定页面,将会该字段的页面作为默认页
        }
    }
}

3、反向代理配置

注:本机 ip 为 192.168.137.128

worker_processes  1;	# 工作进程数,最好与 CPU 核数相同

events {
    worker_connections  1024;	# 每个进程可以接受多少个连接
}

http {
    include       mime.types;				# 引入 http mime 类型,表示文件是什么类型,mime.types 是 conf 目录下的一个文件
    default_type  application/octet-stream;	# 如果 mime 类型没匹配上,默认使用二进制流的方式传输

    sendfile        on;						# 使用 linux 的 sendfile(socket,file,len) 高效网络传输,也就是零拷贝

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
	server {
        listen       80;					# 监听的端口号
        server_name  localhost;				# 主机名,可以配置域名或主机名,可以配置多个,用 ',' 隔开  
		
		# 反向代理配置
		# 在浏览器输入 192.168.137.128,将会转发到 192.168.137.129;
        location / {
            proxy_pass http://192.168.137.129;
        }
        
        error_page   500 502 503 504  /50x.html;	# 服务端发生错误,将访问 /50x.html
		# 如果在 www.baidu.com/50x.html 没有找到 50x.html,将会跳转到  www.baidu.com/html/50x.html
        location = /50x.html {
            root   html;		
        }
    }
}

4、负载均衡配置

worker_processes  1;	# 工作进程数,最好与 CPU 核数相同

events {
    worker_connections  1024;	# 每个进程可以接受多少个连接
}

http {
    include       mime.types;				# 引入 http mime 类型,表示文件是什么类型,mime.types 是 conf 目录下的一个文件
    default_type  application/octet-stream;	# 如果 mime 类型没匹配上,默认使用二进制流的方式传输

    sendfile        on;						# 使用 linux 的 sendfile(socket,file,len) 高效网络传输,也就是零拷贝

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    # 负载均衡配置,将会以轮询的方式转发下面的网站
	# 即 ABABAB...
	upstream httpds {
        server  192.168.137.129:80;	# A 服务器
        server  192.168.137.130:80;	# B 服务器
    }
    
	server {
        listen       80;					# 监听的端口号
        server_name  localhost;				# 主机名,可以配置域名或主机名,可以配置多个,用 ',' 隔开  
		
		location / {
            proxy_pass http://httpds;	# 转发到 httpds 中的服务器中
        }
        
        error_page   500 502 503 504  /50x.html;	# 服务端发生错误,将访问 /50x.html
		# 如果在 www.baidu.com/50x.html 没有找到 50x.html,将会跳转到  www.baidu.com/html/50x.html
        location = /50x.html {
            root   html;		
        }
    }
}

upstream 用法

1、热备:如果有2台服务器,当 A 服务器发生事故时,才启用 B 服务器提供服务。服务器处理请求的顺序:AAAAA,突然 A 服务器挂啦,BBBBBBBB…

upstream httpds {
	server  192.168.137.129:80;
	server  192.168.137.130:80 backup; # backup 表示热备,只有 129 服务器断线了,才会把请求转发到 130
}

2、加权轮询:根据配置的权重大小而分发给不同服务器不同数量的请求。如果不设置,则默认为 1。下面服务器的请求顺序为:ABBABBABBABBABB…

upstream httpds {
	server  192.168.137.129:80 weight=1;	# A 服务器
	server  192.168.137.130:80 weight=2;	# B 服务器
}

3、ip_hash:nginx会让相同的客户端ip请求相同的服务器

upstream httpds {
	server  192.168.137.129:80;	# A 服务器
	server  192.168.137.130:80;	# B 服务器
	ip_hash;
}

5、动静分离

为了减轻程序服务器(Tomcat,Jboss等)的负载,对于静态资源,如图片、js、css等文件,可以缓存反向代理服务器中,就像 redis 一样。

这样浏览器在请求一个静态资源时,反向代理服务器就可以直接处理,而不用将请求转发给后端服务器,而对于用户请求的动态文件,如servlet、jsp,则转发给Tomcat,Jboss服务器处理,这就是动静分离,即动态文件与静态文件的分离。
注:动静分离只适用于中小型网站。

worker_processes  1;	# 工作进程数,最好与 CPU 核数相同

events {
    worker_connections  1024;	# 每个进程可以接受多少个连接
}

http {
    include       mime.types;				# 引入 http mime 类型,表示文件是什么类型,mime.types 是 conf 目录下的一个文件
    default_type  application/octet-stream;	# 如果 mime 类型没匹配上,默认使用二进制流的方式传输

    sendfile        on;						# 使用 linux 的 sendfile(socket,file,len) 高效网络传输,也就是零拷贝

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    # 负载均衡配置,将会以轮询的方式转发下面的网站
	# 即 ABABAB...
	upstream httpds {
        server  192.168.137.129:80;	# A 服务器
        server  192.168.137.130:80;	# B 服务器
    }
    
	server {
        listen       80;					# 监听的端口号
        server_name  localhost;				# 主机名,可以配置域名或主机名,可以配置多个,用 ',' 隔开  
		
		location / {
            proxy_pass http://httpds;	# 转发到 httpds 中的服务器中
        }
        
        # 正则匹配:~ 表示使用正则, * 表示忽略大小写,| 表示或
        # 即请求路径中包含 /css 、/js 、/images 、/fonts 都匹配下面的根目录(静态文件)
        location ~*/(css|js|images|fonts) {
            root html;  # root 中不需要在写 html/css 这样的路径,只要匹配上了,会自动在 html 后面加 上匹配的字符串 ,如 html/css
            index  index.html index.htm;
        }
        
        error_page   500 502 503 504  /50x.html;	# 服务端发生错误,将访问 /50x.html
		# 如果在 www.baidu.com/50x.html 没有找到 50x.html,将会跳转到  www.baidu.com/html/50x.html
        location = /50x.html {
            root   html;		
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值