Nginx配置反向代理


1、准備工作

1.1、Linux上安裝tomcat

首先需下載 tomcat 安裝包,可參考:https://www.cnblogs.com/wenxuehai/p/14133196.html#_label2,注意,下載 tar.gz 格式的安裝包。

將安裝包上傳至 Linux 系統的 /usr/src 目錄下,通過 SSH Secure File Transfer Client 工具可以很方便地將本地的 tomcat 壓縮包上傳至 Linux 系統。

然后通過 tar -xvf 文件名 命令將壓縮包解壓即可。

 

1.2、啟動 tomcat

切換至 tomcat 目錄下的 bin 目錄,然后通過 ./startup.sh 命令打開該腳本文件,啟動tomcat。

實例:

注意,Linux 系統中默認自動安裝了 java,如果沒有,我們需要自己安裝。

 

1.2.1、校驗啟動是否成功

tomcat 啟動過后,我們可以在本地 window 的瀏覽器中通過 linux系統的IP:8080 來訪問 tomcat 的默認 ROOT 項目:

如果訪問失敗,則可能是 Linux 中未開啟 8080 端口的對外訪問,此時需開啟該端口的對外訪問。

通過 firewall-cmd --add-port=8080/tcp --permanent 命令開啟端口,通過 firewall-cmd --list-all 命令可查看已開啟的端口。實例如下:

 

1.2.2、查看tomcat的日志文件

在啟動過后,我們可以查看 tomcat 的日志文件,看啟動是否正常。

首先需切換至 tomcat 根目錄下的 logs 目錄,然后通過 tail -f 文件名 命令來查看該文件夾下的 catalina.out 文件。

可以看到,tomcat 已正常啟動。 

 

2、反向代理分析

Nginx 的反向代理原理圖:

我們需要實現的效果是,在本地訪問 www.123.com 能直接訪問到 tomcat 里面的項目。首先我們是先訪問了 Nginx 服務器,通過 Nginx 的反向代理,最后訪問到 tomcat 里面的資源。由此實現了反向代理,客戶端無法感知到實際上訪問到的服務器。

因為我們把 Nginx 和 tomcat 安裝在了同一台服務器上,所以代理后的結果只是改了一個端口號。

 

3、修改本地的hosts文件

首先如何通過訪問 www.123.com 能映射到我們的 Linux 服務器地址呢?此時我們修改本地 window 系統中的 hosts 文件。

hosts 文件的作用:用戶在瀏覽器中輸入一個網址時,系統會首先自動從本地的 Hosts 文件中尋找對應的IP地址,一旦找到,系統會立即通過對應的 ip 來打開對應網頁。如果沒有找到,則系統會將網址提交至因特網,由因特網中的 域名系統DNS 進行DNS域名解析,由此獲得相應的IP地址。

所以我們在本地修改 hosts 文件,來將 www.123.com 映射到我們的 Linux ip 地址。本地訪問 www.123.com 實際就能訪問到 Linux 系統。

hosts 文件放在系統的 C:\Windows\System32\drivers\etc 目錄下:

直接修改該文件,在該文件的后面添加從 www.123.com 到 Linux 系統的 ip 的映射,格式:ip  域名

如下:

修改完之后,在 window 本地瀏覽器可以通過 www.123.com:80 訪問到 Nginx:

也可以通過 www.123.com:8080 訪問到 tomcat 項目:

 

3.1、無法保存hosts文件

修改 hosts 文件可能無法保存或者提示沒有權限,此時可參考:http://www.xitongcheng.com/jiaocheng/win10_article_12937.html  修改權限,然后還需在該文件的屬性將只讀選項取消掉,如下:

 

4、配置Nginx反向代理

通過修改 Nginx 的配置文件可以實現反向代理。

首先切換至 Nginx 的安裝目錄下。之前我們是把 Nginx 的安裝包放在了 /usr/src 目錄下進行安裝操作,安裝成功后 Nginx 的安裝目錄會在 /usr/local 上。Linux 系統中,一般程序安裝完成后,都會安裝在 /usr/local 目錄下。

在 Nginx 的安裝目錄找到下 conf 文件夾,該文件夾下的 nginx.conf 文件即是 Nginx 的配置文件,通過 vi 來直接修改:

通過 vi 命令打開 nginx.conf 文件我們就可以進行修改。首先按 i 進行插入,然后進行修改,修改完成后通過 :wq 命令來保存並退出。

修改內容:

proxy_pass 后面配置的就是代理轉發的路徑,我們也可以將 proxy_pass 后面的一串改為: http://192.168.32.128:8080;  

 

配置完之后,我們就可以通過 ww.123.com 來直接能訪問到 tomcat 項目,而不是訪問到 Nginx,因為此時已經經過了代理轉發。

 

4.1、完整的nginx.conf配置文件

完整 nginx.conf 文件如下:

#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.32.128;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass  http://127.0.0.1:8080;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        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;
    #    }
    #}

}

 

5、配置根據不同URL代理到不同路徑

上面的 Nginx 配置將 Nginx 服務器的 80 端口的請求轉發至 Nginx 服務器 8080 端口。實際上,在 Nginx 的配置當中,http 中可以有多個 server 選項,並且一個 server 選項中也可以有多個 location,可以配置不同的 location 代理轉發至不同的路徑。

比如想要代理多個項目,不同的 url 就代理轉發至不同的項目的服務器地址。

下面的流程圖是在跟同一台 Nginx 服務器上啟動了兩個 tomcat 來模擬兩台不同的服務器:

Nginx 作為代理服務器,我們希望達到的效果是 Nginx 匹配到不同的路徑能代理轉發至不同的地址。

實現步驟如下:

先在 Nginx 服務器上啟動兩個 tomcat,分別是 tomcatA 和 tomcatB。同台服務器上有多個 tomcat ,需要注意修改 tomcat 的端口以免沖突,不僅僅是 8080 端口,其他端口也需一並修改否則會有沖突。

然后我們在 8081 端口的 tomcatA 服務器的 webapps 新建一個 systemA 的項目,在 8082 端口的 tomcatB 服務器的 webapps 新建一個 systemB 的項目。

最后修改 Nginx 的配置,增加一個 server 模塊,監聽 9001 端口:

server {
    listen       9001;
    server_name  192.168.32.128;

    location ~ /systemA/ {
        proxy_pass  http://127.0.0.1:8081;
    }
    
    location ~ /systemB/ {
        proxy_pass  http://127.0.0.1:8082;
    }
}

此時,我們訪問 http://192.168.32.128:9091/systemA 將會訪問 tomcatA 的 systemA 項目,訪問 http://192.168.32.128:9091/systemB 將會訪問 tomcatB 的 systemB 項目。

我們可以發現,Nginx 在進行代理轉發時,只是將配置的目標 url 替代監聽 ip 地址和端口號,后面的路由等部分(比如上面的 /systemA)仍會拼接到瀏覽器最終訪問的 url 中進行訪問資源。

 

5.1、完整的配置文件

Nginx完整配置文件:

#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.32.128;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass  http://127.0.0.1:8080;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    server {
        listen       9001;
        server_name  192.168.32.128;

        location ~ /systemA/ {
            proxy_pass  http://127.0.0.1:8081;
        }
        
        location ~ /systemB/ {
            proxy_pass  http://127.0.0.1:8082;
        }
    }


    # 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;
    #    }
    #}

}

 


免責聲明!

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



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