Nginx反向代理的使用场景及相关配置

Nginx的反向代理使用场景之一

1、场景描述

一个前端服务(vue服务/uniapp小程序),通过域名访问后端服务,后端服务分成两个模块,一个是ssm架构,一个是springboot架构,如何访问?

2、解决方案

采用Nginx做反向代理(Nginx详解
在服务器端安装Nginx,在nginx.conf文件中进行以下配置
注意: 如果端口被占用则需要配置新的端口
nginx.conf文件内容如下,详情在里面有注释

#user  nobody;
worker_processes  1;

#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       8080;# 监听的端口,如果被占用则需要换掉
        server_name  ######.com;# 要访问的域名,如baidu.com

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            rewrite ^(.*)$ https://######.com:443/$1 permanent; # 符合匹配规则的请求,重定向到该地址
        }

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


    # ssl 配置
    server {
       listen       443 ssl; # 默认端口
       server_name  www.######.com; # 要访问的域名,如 baidu.com
	# ssl证书地址:下载下来的ssl证书中有
	#指定文件所在路径,如果写相对路径,必须把该文件和nginx.conf文件放到一个目录下。
       ssl_certificate      cert22120978d437b770f96f83_www.rayoosystem.com_NGINX.crt;
       ssl_certificate_key  cert22120978d437b770f96f83_www.rayoosystem.com_NGINX.key;
       ssl_session_cache    shared:SSL:1m;
       ssl_session_timeout  5m;
	   #root xxxxxx; # 直接访问域名时,默认跳转的页面地址。因为小程序是发布在微信公众平台上,所以这里不用配置
       ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
       ssl_prefer_server_ciphers  on;
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		
		# 路由配置:以下有两个路由配置,
		# 一个用来访问后端服务A接口:http://服务器ip:服务A端口 + 后续接口
		# 另一个来访问后端服务B接口:http://服务器ip:服务B端口/a/ + 后续接口
		# 通过以下两个路由配置将实现以下效果:
		# 后端有两个接口:
		#服务A的一个接口:http://111.222.333.444:8081/user/login(服务器访问)
		#服务B的一个接口:http://111.222.333.444:8082/a/user/getInfo(服务器访问)
		#则前端通过域名访问,将自动匹配到后端的这两个服务,如:
		#前端配置访问路径:http://www.######.com/user/login ---》服务A(http://111.222.333.444:8081/user/login)
		#前端配置访问路径:http://www.######.com/a/user/getInfo ---》服务B(http://111.222.333.444:8082/a/user/getInfo)
		location / {
		
			# 用来设定被代理服务器接收到的header信息 以下 proxy_set_header 的配置为标配
            proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			
			# 路径重写 去除 /api 前缀
			# rewrite ^/api/(.*) /$1 break;
			proxy_pass http://服务器ip:服务A端口; # 代理转发
			#示例:proxy_pass http://111.222.333.444:8081;
       }
	   
	   location /a/ {
		
			# 用来设定被代理服务器接收到的header信息 以下 proxy_set_header 的配置为标配
            proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			
			# 路径重写 去除 /api 前缀
			# rewrite ^/api/(.*) /$1 break;
			proxy_pass http://服务器ip:服务B端口; # 代理转发
			#示例:proxy_pass http://111.222.333.444:8082;
       }
    }
}

注意点: 如果遇到访问失败跨域问题,如果配置没问题,有可能服务器相关端口未开放,去云端检查相关端口是否放开。

3、关键点

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值