nginx 端口映射


一、通過NGINX做端口映射

備注:做完映射后,輸入IP(URL)即可,不用輸入端口,走默認80端口

第一步:

進去NGINX配置文件
cd /etc/nginx/sites-enabled

第二步:

查看是否有文件 default(該文件可以為其他,根據需要命名)

ls

第三步:

編輯 default 文件
# 以下是在一個 server 中
location / {
              # First attempt to serve request as file, then
              # as directory, then fall back to displaying a 404.
              # try_files $uri $uri/ =404;
              client_max_body_size 100M;
              client_body_buffer_size 128k;
#
#               add_header 'Access-Control-Allow-Origin' '*';
#               add_header 'Access-Control-Allow-Methods'
'GET,POST,OPTIONS,PUT,HEAD,PATCH,DELETE';
              proxy_pass http://127.0.0.1:12000;
      }

二、整個NGINX配置的樣例

server {
      listen 80 default_server;
      listen [::]:80 default_server;
root /var/www/html;

      # Add index.php to the list if you are using PHP
      index index.html index.htm index.nginx-debian.html;

      server_name _;

      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_redirect off;
      if ( $request_method = OPTIONS ) {
          return 200 '';
      }
      # 允許跨越
      add_header "Access-Control-Allow-Origin" "*";
      # 允許的請求方法
      add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, PUT, HEAD, PATCH, DELETE";
      # 頭部中允許的參數
      add_header "Access-Control-Allow-Headers" "Content-Type, Authorization, X-Requested-With, Session-Id";
# 映射的 12000 端口開的服務
      location / {
              client_max_body_size 100M;
              client_body_buffer_size 128k;
              proxy_pass http://127.0.0.1:12000;
      }
      # 映射的 8000 並且 URL 以 /api 開始的路由
      location /api {
                      client_max_body_size 100M;
                      client_body_buffer_size 128k;
                      proxy_pass http://127.0.0.1:8000;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM