Nginx 虛擬主機、反向代理與代理緩存

一、虛擬主機

環境:

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[root@localhost ~]# ifconfig |grep inet
        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255

安裝配置nginx,見我以前寫的文章
https://blog.csdn.net/oToyix/article/details/106212135

案例、配置虛擬主機

1、創建網站代碼目錄及新建首頁文件

[root@localhost conf]# mkdir ../html/yjy
# echo "This is www.yyjy.com">../html/yjy/index.html 

2、配置主配置文件-添加引用

#vim /usr/local/nginx/conf/nginx.conf
include /usr/local/nginx/conf/vhost/*.conf; 
此行放在http {} 塊中

3、虛擬主機配置文件

[root@localhost conf]# mkdir vhost
[root@localhost conf]# vim vhost/www.yjy.com.conf
server{
        listen 80;
        server_name www.yjy.com;
        location / {
                root html/yjy;
                index index.html;
        }
}
[root@localhost conf]# nginx -s reload  重啓nginx

4、C:\Windows\System32\drivers\etc修改此文件

192.168.1.111	www.yjy.com	111.yjy.com
192.168.1.107	107.yjy.com
添加至最後,這裏因爲要做下面實驗,一起寫了

5、瀏覽器訪問
在這裏插入圖片描述

二、反向代理

環境:
代理服務器:192.168.1.111
後端服務器 :192.168.1.107

案例一
客戶端瀏覽器上訪問www.yjy.com得到結果”This is 107.yjy.jcom”,並得到真實遠程客戶端口的信息,如IP等

1、在111服務器上配置

[root@localhost ~]#vim /usr/local/nginx/conf/nginx.conf
include /usr/local/nginx/conf/vhost/*.conf; 
此行放在http {} 塊中
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.yjy.com.conf 
server{
        listen 80;
        server_name www.yjy.com;

        location / {
                proxy_pass http://107.yjy.com;
        }
}  

2、在107服務器上配置

創建107.yjy.com網站代碼目錄
[root@localhost ~]# mkdir /usr/local/nginx/html/107.yjy.com 
新建首頁文件
[root@localhost ~]# echo "This is 107.yjy.com">/usr/local/nginx/html/107.yjy.com/index.html
主配置文件上添加引用
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    include /usr/local/nginx/conf/vhost/*.conf;
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/107.yjy.con.conf 
server{
        listen 80;
        server_name 107.yjy.com;
        location / {
                root html/107.yjy.com;
                index index.html;
        }
}
~ 

3、效果
在這裏插入圖片描述

案例二、配置反向代理,向真實服務器107傳遞遠程客戶端信息
配置111代理服務器

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.yjy.com.conf
server{
        listen 80;
        server_name www.yjy.com;

        location / {
                proxy_pass http://192.168.1.107;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

配置107後端服務器主配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_real_ip" "$http_x_forwarded_for"';

access_log  logs/access.log  main;


效果:
1、在瀏覽器上輸入www.yjy.com,得出’This is 107.yjy.com’

2、在107上查看訪問日誌,可以得出遠程客戶端的真實IP
#tailf /usr/local/nginx/logs/access.log
在這裏插入圖片描述

三、部署nginx代理緩存服務器,並驗證緩存是否生效

1、配置111代理配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.yjy.com.conf

  1 proxy_cache_path /usr/local/nginx/cache max_size=10g levels=1:2 keys_zone=nginx_cache:10m inactive=10m use_temp_path=off;
  2 
  3 server{
  4         listen 80;
  5         server_name www.yjy.com;
  6 
  7         location / {
  8                 #root html/yjy;
  9                 #index index.html;
 10                 proxy_pass http://192.168.1.107;
 11                 proxy_set_header Host $http_host;
 12                 proxy_set_header X-Real-IP $remote_addr;
 13                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 14                 proxy_cache nginx_cache;#定義緩存空間,與第一行值要一致
 15                 proxy_cache_key $host$uri$is_args$args;定義何時去緩存空間拿數據
 16                 proxy_cache_valid 200 302 1d;正確訪問信息及重定向 保存1天,超過1天將刪除文件
 17         }
 18 }
~     

[root@localhost html]# nginx -s reload 重啓服務
2、瀏覽器訪問http://www.yjy.com/test.jpg
在這裏插入圖片描述
3、查看nginx緩存空間/usr/local/nginx/cache
在這裏插入圖片描述
有緩存文件產生,證明代理緩存成功。

注:
/usr/local/nginx/cache #緩存資源存放路徑
levels #設置緩存資源的遞歸級別,默認爲
levels=1:2,表示Nginx爲將要緩存的資源生成的key從後依次設置兩級保
存。
key_zone #在共享內存中設置一塊存儲區域來存放緩存的key和
metadata,這樣nginx可以快速判斷一個request是否命中或者未命中緩存,
1m可以存儲8000個key,10m可以存儲80000個key
max_size #最大cache空間,如果不指定,會使用掉所有disk
space,當達到配額後,會刪除不活躍的cache文件
inactive #未被訪問文件在緩存中保留時間,本配置中如果10分鐘未被訪問則不論狀態是否爲expired,緩存控制程序會刪掉文件。
inactive默認是10分鐘。需要注意的是,inactive和expired配置項的含義是不同的,
expired只是緩存過期,但不會被刪除,inactive是刪除指定時間內未被訪問
的緩存文件
use_temp_path #如果爲off,則nginx會將緩存文件直接寫入指定的
cache文件中,而不是使用temp_path存儲,official建議爲off,避免文件
在不同文件系統中不必要的拷貝
proxy_cache #啓用proxy cache,並指定key_zone。如果
proxy_cache off表示關閉掉緩存。

------------------end

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