美文网首页
nginx端口转发

nginx端口转发

作者: 向前Hell | 来源:发表于2018-07-28 09:34 被阅读0次
@author : DAN
@datetime : 2018/07/28

我们常用的端口有 80/443 端口
80端口对应着HTTP,443端口对应着HTTPS
配置好 nginx 之后,可以查看 nginx.conf 配置,准确记录了80端口和443端口的配置,80端口默认开启,443端口需自行配置解封
下面介绍端口转发

nginx 端口使用

操作系统中对外开放的端口有限,当我们需要使用其他端口的时候,有两种方式可以做到:

  • 开放新端口
  • 端口转发
  1. 开放新端口
    将端口暴露直接使用,nginx配置文件直接将80端口server复制,修改端口等必要信息,重启nginx服务即可生效

  2. 端口转发
    我们正常通过端口进行访问,使用upstream进行端口转发,方式有:

    1. 对端口进行全局转发
    • ip访问
    upstream    web{
        // 例如:server  127.0.0.1:5000
        // 例如:server  219.231.0.***
        server  <Your IP[,:Port]>  
    }
    server {
      listen       80;
      server_name  _;
    
      location / {
          proxy_pass  http://web;
      }
    
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
    }
    
    • 域名访问
    api.<Your Domain>.com   5000;
    server {
        listen       80;
        server_name  api.<Your Domain>.com;
        access_log logs/flask.log;
        error_log logs/flask.error;
    
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:5000;
        }
    }
    
    1. 对端口进行部分转发
    server {
      listen 80;
      server_name <Your Domain>;
      underscores_in_headers on;
      root /usr/share/nginx/html/xiaoan;
      location / {
          if ($request_uri ~* "^\/test"){
                  #add_header Access-Control-Allow-Origin *;
                  add_header Access-Control-Allow-Headers X-Requested-With,content-type,cert,t;
                  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
              proxy_pass http://dobelltest.com;break;
          }
          #如果URL带/dos的话就配到Tomcat
          if ($request_uri ~* "^\/dos"){
              #add_header Access-Control-Allow-Origin *;
              add_header Access-Control-Allow-Headers X-Requested-With,content-type,cert,t;
              add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
              proxy_pass http://<Your Domain>; break;
              break;
          }
          if ($request_uri ~* "^\/registration"){
              add_header Access-Control-Allow-Headers X-Requested-With,content-type,cert,t;
              add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
              proxy_pass http://127.0.0.1:8080;
              break;
          }
          if ($request_uri ~* "^\/pay"){
              #add_header Access-Control-Allow-Origin *;
              add_header Access-Control-Allow-Headers X-Requested-With,content-type,cert,t;
              add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
              proxy_pass http://dobell.com; break;
              break;
          }
          #如果URL带/phpstatic就配到Apache
          if ($request_uri ~* "^\/phpstatic"){
              add_header Access-Control-Allow-Origin *;
              add_header Access-Control-Allow-Headers X-Requested-With,content-type,cert,t;
              add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
              proxy_pass http://php.com; break;
          }
          # 如果URL里有/honest就转发到8090端口
          if ($request_uri ~* "^\/honest"){
                  #add_header Access-Control-Allow-Origin *;
                  add_header Access-Control-Allow-Headers X-Requested-With,content-type,cert,t;
                  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
              proxy_pass http://<Your Domain>;break;
          }
      }
    }
    
    

相关文章

  • nginx安全问题

    一、nginx 默认转发至现有虚拟主机nginx 在开启某些监听端口时,必须关闭默认端口转发(比如: 你nginx...

  • nginx bind() to 0.0.0.0:9999

    内部的端口跟nginx转发的端口不能一致

  • nginx端口转发

    移出/etc/nginx/conf.d/目录下默认的default.conf文件vim /etc/nginx/co...

  • nginx端口转发

    我们常用的端口有 80/443 端口80端口对应着HTTP,443端口对应着HTTPS配置好 nginx 之后,可...

  • Nginx 端口转发

    想尝试Arachni这款扫描器,下载运行之后发现默认的地址在http://127.0.0.1:9292, 由于是在...

  • nginx 端口转发

    测试nginx端口转发 环境:ubuntu 18.04 LTS(cat /etc/issue)1.安装python...

  • nginx端口转发

    端口转发配置文件 在/etc/nginx/conf.d/目录下创建*.conf文件 这个配置文件将192.168....

  • Nginx入门

    1.root安装nginx需要依赖的库 2.编译nginx 3.Nginx做非80端口转发 要做转发,可以使用Ng...

  • nginx配置端口转发

    1. 将域名转发到本地端口 这样访问 http://baidu.com[http://baidu.com] 时就会...

  • uwsgi配置

    [uwsgi] # 这个配置随意 http = 9000 # 接受nginx转发的端口 socket = 127....

网友评论

      本文标题:nginx端口转发

      本文链接:https://www.haomeiwen.com/subject/xrnnmftx.html