【Nginx】 Nginx實現端口轉發


什么是端口轉發

當我們在服務器上搭建一個圖書以及一個電影的應用,其中圖書應用啟動了 8001 端口,電影應用啟動了 8002 端口。此時如果我們可以通過

localhost:8001    //圖書
localhost:8002    //電影

 

  但我們一般訪問應用的時候都是希望不加端口就訪問域名,也即兩個應用都通過 80 端口訪問。但我們知道服務器上的一個端口只能被一個程序使用,這時候如何該怎么辦呢?一個常用的方法是用 Nginx 進行端口轉發。Nginx 的實現原理是:用 Nginx 監聽 80 端口,當有 HTTP 請求到來時,將 HTTP 請求的 HOST 等信息與其配置文件進行匹配並轉發給對應的應用。例如當用戶訪問 book.douban.com 時,Nginx 從配置文件中知道這個是圖書應用的 HTTP 請求,於是將此請求轉發給 8001 端口的應用處理。當用戶訪問 movie.douban.com 時,Nginx 從配置文件中知道這個是電影應用的 HTTP 請求,於是將此請求轉發給 8002 端口的應用處理。一個簡單的 Nginx 配置文件(部分)如下面所示:

#配置負載均衡池
#Demo1負載均衡池
upstream book_pool{
    server 127.0.0.1:8001;
}
#Demo2負載均衡池
upstream movie_pool{
    server 127.0.0.1:8002;
}

#Demo1端口轉發
server {
    listen       80;
    server_name  book.chanshuyi.com;
    access_log logs/book.log;
    error_log logs/book.error;
    
    #將所有請求轉發給demo_pool池的應用處理
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://book_pool;
    }
}
#Demo2端口轉發
server {
    listen       80;
    server_name  movie.chanshuyi.com;
    access_log logs/movie.log;
    error_log logs/movie.error;
    
    #將所有請求轉發給demo_pool池的應用處理
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://movie_pool;
    }
}

上面這段配置實現了:

1、當用戶訪問的域名是:http://book.chanshuyi.com 時,我們自動將其請求轉發給端口號為 8001 的 Tomcat 應用處理。

2、當用戶訪問的域名是:http://movie.chanshuyi.com 時,我們自動將其請求轉發給端口號為 8002 的 Tomcat 應用處理。

  上面的這種技術實現就是端口轉發。端口轉發指的是由軟件統一監聽某個域名上的某個端口(一般是80端口),當訪問服務器的域名和端口符合要求時,就按照配置轉發給指定的 Tomcat 服務器處理。我們常用的 Nginx 也有端口轉發功能。

 

 

 

例如我的一個80端口轉發到8080tomcat服務器的配置:(注意紅字地方即修改的地方)

linux下面修改的文件是:  /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

 upstream forwardport{ server 127.0.0.1:8080; }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
    server_name qiaoliqiang.cn
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

 location / { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://forwardport; }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for ap TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

 

當然也可以iptables進行轉發:(80轉到8080處理)

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

 


免責聲明!

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



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