Nginx的反向代理和動靜分離


title: Nginx的反向代理和動靜分離
date: 2019-05-26
tags: nginx

Nginx的反向代理和動靜分離

以我的項目爲例,展示反向代理和動靜分離的過程,具體概念自行百度
每個人的項目情況不同,我在摸索兩者的過程中搜索了很多相關的東西,類似的可以參考,但是不能照搬,不然會發生莫名其妙的錯誤

首先安裝nginx
這個寫的挺好 https://www.cnblogs.com/taiyonghai/p/6728707.html

我在這裏tomcat端口是80 nginx端口改成了8090
並把服務ip改成了阿里雲服務器上的公網ip
在這裏插入圖片描述
瀏覽器訪問http://47.100.161.71:8090/後有:
在這裏插入圖片描述
這說明nginx安裝和部署成功

接着進行反向代理,只是演示代理tomcat服務器(這裏的前提是筆者已經在阿里雲服務器上安裝好tomcat並把項目跑起來了),反向代理把tomcat的請求交給nginx去處理,

打開nginx配置文件 nginx.conf
添加upstream模塊起名lagoon(名字隨意)

upstream lagoon{
        server 47.100.161.71:80;
       }

在這裏插入圖片描述
設定server 爲阿里雲公網ip+tomcat端口號
接着爲nginx設置請求路徑

location /tmall {
           # root   html;
           # index  index.html index.htm;
           proxy_pass http://lagoon/tmall;
        }

在這裏插入圖片描述
這個意思是當nginx捕捉到請求路徑/tmall時,把他跳轉到http://lagoon/tmall
也就是tomcat上能夠運行起來的項目主頁路徑
其中lagoon就是 47.100.161.71:80
保存配置文件並重啓nginx(方法百度)
如下,這是tomcat上運行的項目
在這裏插入圖片描述
回車:
在這裏插入圖片描述
進入了項目主頁
接着試試nginx代理後的效果
在這裏插入圖片描述
回車:
在這裏插入圖片描述
同樣進入了項目主頁,簡單的說就是nginx捕捉到/tmall請求,把它跳轉到tomcat裏去展示,但是同時又是自己的路徑地址。
這是一種很簡單的代理

有關動靜分離
讓圖片資源,css js等在tomcat里加載效率比較低
我們讓nginx處理這些靜態資源,當然動態的還是tomcat來做

一個流程是:當該項目有請求的靜態資源時,不用tomcat去聯網加載,而是用服務器本地的靜態資源去展示
這個方法很多博客上說到過,不過僅僅是用nginx來搭建圖片服務器而言,不適用於我的項目場景
一開始嘗試了很多配置之後,用谷歌瀏覽器右鍵審查圖片加載狀況,一開始發現403,資源請求被拒絕,這個時候把nginx配置裏的用戶設置爲root,在這裏插入圖片描述
使得nginx具有最高權限去工作
接着發現項目裏的圖片還是出不來,審查爲404
在這裏插入圖片描述
類似以上這種後綴匹配不適用於我的項目場景,經審查元素和console後,我發現當前項目圖片的請求路徑是
http://47.100.161.71:8090/tmall/img/productSingle_small/1322.jpg
這種形式的
也就是如圖(演示例子,當時是出不來的)
在這裏插入圖片描述
類比請求路徑/tmall
我做了以下配置文件修改
在這裏插入圖片描述
意思爲如果請求路徑爲/tmall/img/時
讓nginx拿本地存在的靜態圖片去顯示
看路徑應該就知道服務器本地存放的靜態圖片就是部署在tomcat webapps下面的項目的圖片存放文件夾
在這裏插入圖片描述
alias的用法自行百度
同理css js的用法一樣
這樣就做到了靜態資源不用tomcat去在容器里加載,這樣速度也慢,效率也低
而是讓所有的靜態資源請求交給nginx,讓nginx從本地存在的靜態資源裏拿出去給大家顯示
下面是配置文件的所有內容


user  root;
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;
    upstream lagoon{
        server 47.100.161.71:80;
       }

    server {
        listen       8090;
        server_name  47.100.161.71;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /tmall {
           # root   html;
           # index  index.html index.htm;
           proxy_pass http://lagoon/tmall;
        }

        location /tmall/img/ {
        alias /usr/local/tomcat/apache-tomcat-8.5.41/webapps/tmall/img/;
        autoindex on;
                                                                                                        }
        #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;
    #    }
    #}

}

最後做好相關修改後重啓nginx訪問項目,審查圖片來源頭部信息(圖片加載會快一些了~)
在這裏插入圖片描述
會發現圖片是通過nginx來把關顯示的
有關nginx的反向代理和動靜分離還有許多需要深入的地方,還有緩存策略等等。
還是需要對nginx進行一個全方位的學習和了解的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章