iT邦幫忙

0

關於「Nginx正向代理Cache配置」問題

前情提要:

在早期一開始的時候,剛入門學習Nginx有參照網路上的資料,做Nginx反代理的Cache功能

代碼如下
vi nginx.conf

http {
    add_header X-Cache $upstream_cache_status; #缓存命中
    proxy_cache_path  /var/nginx/cache  levels=1:2    
    keys_zone=STATIC:10m   inactive=24h  max_size=1g;
     client_max_body_size 10G;
     server {
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass             http://127.0.0.1;
            proxy_set_header       Host $host;
            proxy_cache            STATIC;
            proxy_cache_valid      200  1d;
            proxy_cache_use_stale  error timeout invalid_header updating
                                   http_500 http_502 http_503 http_504;
        }
    }

vi scott.conf

upstream proxy {

        server  172.16.16.8:80;
}

server {
    listen      80 default;
    server_name www.scott.com;

    location / {

        proxy_pass  http://proxy;

        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto https;
        proxy_set_header     X-Forwarded-Host $host;

proxy_cache            STATIC;
  proxy_cache_valid      200  1d;
  proxy_cache_use_stale  error timeout invalid_header updating
                         http_500 http_502 http_503 http_504;

}

}

但是後來需要改用正向代理的方式的時候,也做一樣的配置,但卻無任何Cache產生,因此經過測試,自己得此結論就是,這個配置方式只適合反向代理

進入正題

因此這兩天都開始往「Nginx正向代理 Cache配置」這個方向去尋找

以下是我正向代理的配置


server {
        listen 80;
        server_name www.jacke.com;
        root /usr/share/nginx/html/web/ ;
        index index.php index.html index.htm;

     # for Laravel ENV
location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        try_files $uri $uri/ /index.php?$query_string;

proxy_cache            STATIC;
  proxy_cache_valid      200  1d;
  proxy_cache_use_stale  error timeout invalid_header updating
                         http_500 http_502 http_503 http_504;

}
}

但陸續找了不少文件都沒有有效的出現Cache,因此參考了Nginx官方文件仍舊沒有效果

因此想上來諮詢各位前輩,聽聽大家意見

也相當願意聽從任何糾正觀念的聲音

畢竟我是自學起家,大多的經驗都來至於網路及自身認為,目前還仍處於不斷修正的階段
/images/emoticon/emoticon41.gif

看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2020-04-24 16:21:13 檢舉
你這設定檔怎麼看都是反向代理吧...
是要cache 你的Laravel 伺服器?

正向代理功用像是proxy
踏雪尋梅 iT邦研究生 5 級 ‧ 2020-04-24 16:32:16 檢舉
哦哦 ... 可能我的觀念有錯誤

認為有做轉導,就是屬於反代理
upstream proxy {

server 172.16.16.8:80;
}

反之就是正代理 ..
正向代理?你的瀏覽器有設定透過Proxy出去嗎?
踏雪尋梅 iT邦研究生 5 級 ‧ 2020-04-24 17:18:18 檢舉
有的,我這邊直接在Google 手動設定 Proxy ,確實有
踏雪尋梅 iT邦研究生 5 級 ‧ 2020-04-24 17:41:22 檢舉
更新進度 我參考https://www.applenice.net/2019/09/13/Nginx-Notes-03/

proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

proxy_cache my_cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 302 304 1d;

proxy_pass http://www.scott.com;

配置,緩存的確有出現,但是index.html 內容是空的

(正常應該要出現 scott Hello)

目前 研究修改中

看起來我沒搞懂 proxy_pass 的運作
fillano iT邦超人 1 級 ‧ 2020-04-24 17:50:55 檢舉
https://www.alibabacloud.com/blog/how-to-use-nginx-as-an-https-forward-proxy-server_595799
踏雪尋梅 iT邦研究生 5 級 ‧ 2020-04-24 19:44:48 檢舉
恩 .... (沉默)
踏雪尋梅 iT邦研究生 5 級 ‧ 2020-04-27 16:00:33 檢舉
暫時用
proxy_pass 結合upstream解決

但似乎這樣做,感覺有點多此一舉
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答