nginx(四) nginx使用demo

下面在windows环境下演示下Nginx的用法

一、代理后端接口:如我本地在9999端口启动一个服务:

现在使用nginx代理到这个端口:此时访问localhost:9991会代理localhost:9999

 

通过代理访问接口:localhost:9999/demo/code/getCode使用localhost:9999 = localhost:9991规则来替换,得到localhost:9991/demo/code/getCode

如果Nginx换个写法:

 server {	
        listen       9991;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://localhost:9999/demo/;
        }

 这时候访问localhost:9991会代理到localhost:9999/demo/,原地址localhost:9999/demo/code/getCod替换下,所以访问代理的url为:

二、代理前端页面:

server {
	
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://localhost:9999;
        }
		
		#访问方式:http://localhost/test/或http://localhost/test
		location ^~  /test{
            alias   F:/aaa;
			index test.html;
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
            ssi on;
            ssi_silent_errors on;
			proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

三、代理静态资源:

server {
	
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://localhost:9999;
        }
		
        #访问方式:http://localhost/file/或http://localhost/file
		location ^~  /file{
            alias   E:/jdk1.8;
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
            ssi on;
            ssi_silent_errors on;
			proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

四、使用同一个Nginx配置文件同时代理多个后端服务、静态资源:


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;  

    server {
	
        listen       80;
        server_name  localhost;

        #访问方式:http://localhost/demo/code/getCode,代理到http://localhost:9999/demo/code/getCode接口
        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://localhost:9999;
        }
		
		
		 #访问方式:http://localhost/mytest/demo/code/getCode,代理到http://localhost:7777/demo/code/getCode接口
		 location /mytest/ {
			proxy_pass http://localhost:7777/;
        }
		
		#访问方式:http://localhost/test/或http://localhost/test
		location ^~  /test{
            alias   F:/aaa;
			index test.html;
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
            ssi on;
            ssi_silent_errors on;
			proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
		
		#访问方式:http://localhost/file/或http://localhost/file
		location ^~  /file{
            alias   E:;
            autoindex on;
            autoindex_exact_size off;
            autoindex_localtime on;
            ssi on;
            ssi_silent_errors on;
			proxy_redirect          off;
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
		
		   
    } 

}

五、使用Nginx实现负载均衡:使用upstream。

 如同一个服务在本地分别启动9999和7777两个端口:

 

修改nginx配置并重启:注意weight和=中间不要有空格

    这时候访问localhost:9991/demo/code/getCode,可以看到本地9999和7777两个端口在轮询调用。

再如:

upstream mydemoservice{
	  server  localhost:7777 weight=5;
	   server localhost:9999 weight=5;
	}

    server {
	
        listen       80;
        server_name  localhost;

        #访问方式:http://localhost/demo/code/getCode,代理到http://localhost:9999/demo/code/getCode接口
        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://localhost:9999;
        }
		
		location /myserver/ {
			proxy_pass http://mydemoservice/;
        }

访问localhost/myserver/demo/code/getCode。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值