Linux中配置端口转发(反向代理)

在conf.d目录下建一个文件, 以conf为结尾(如果没有conf.d目录,就自己新建一个)

server {
  listen 80;
  server_name 127.0.0.1;  #这个IP是你服务器的IP
location /dongke {
    root   /data/www/;
    index  index.html index.htm;
  }
    location /{
      add_header Access-Control-Allow-Origin *;
      add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
      add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
      if ($request_method = 'OPTIONS') {
       return 204;
      }

      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   Host      $http_host;
      proxy_set_header X-NginX-Proxy true;
#      proxy_pass         http://127.0.0.1:8888/;
 proxy_pass http://127.0.0.1:8769/;   #这里的端口就是你需要隐藏的端口
      proxy_redirect off;
  }
}

这是一个配置文件

然后在nginx.conf中加入配置

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;  #就是这个路径,刚刚配的配置文件
}

  

posted @ 2020-05-15 18:04  一个九零後的程序猿  阅读(2873)  评论(0编辑  收藏  举报