nginx 映射ip端口服务

笔者只对外开放了一个ip端口,但实际使用不止一个端口,那么需要把ip端口映射出来。这里使用Nginx来映射ip端口。比如将前端系统的ip端口映射出去了,还需要后台的ip端口,这样前后端才能结合使用

  • 通过后台服务名(dwiot-cloud)来分发到后台服务ip端口,这样可以直接通过外网访问后台服务。通过已开放的ip端口+dwiot-cloud就相当于在访问219:8088/dwiot-cloud一样
    在这里插入图片描述

  • 第二个location是为前端页面配置的,将前端页面打包放在html下的dwiot-web(自创的目录)下,这样默认192.168.5.219:80就是在访问dwiot-web下的前端界面(上截图所示)

  • 完整配置:

     
      #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       80;
      	server_name  192.168.5.219;
    
      	#后台服务配置,配置了这个location便可以通过http://域名/dwiot-cloud/xxxx 访问		
      	location ^~ /dwiot-cloud {
      		proxy_pass              http://192.168.5.219:8088/dwiot-cloud/;
      		proxy_set_header        Host 192.168.5.219;
      		proxy_set_header        X-Real-IP $remote_addr;
      		proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      	}
      	#解决Router(mode: 'history')模式下,刷新路由地址不能找到页面的问题
      	location / {
      		root   html/dwiot-web;
      		index  index.html index.htm;
      		if (!-e $request_filename) {
      			rewrite ^(.*)$ /index.html?s=$1 last;
      			break;
      		}
      	}
      }
      
    
    
      # another virtual host using mix of IP-, name-, and port-based configuration
      #
      server {
          listen       8000;
      #    listen       somename:8080;
          server_name  192.168.5.219;
    
          location /web-prototype {
              root   html/web-prototype;
              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;
      #    }
      #}
    }
    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王子様~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值