者:小不點(diǎn)啊
來(lái)源:www.cnblogs.com/leeSmall/p/9356535.html
一、Nginx Rewrite 規(guī)則
Rewrite規(guī)則含義就是某個(gè)URL重寫(xiě)成特定的URL(類(lèi)似于Redirect),從某種意義上說(shuō)為了美觀或者對(duì)搜索引擎友好,提高收錄量及排名等。
語(yǔ)法:
rewrite <regex> <replacement> [flag]
關(guān)鍵字 || 正則 || 替代內(nèi)容 || flag標(biāo)記
Rewrite規(guī)則的flag標(biāo)記主要有以下幾種:
last和break用來(lái)實(shí)現(xiàn)URL重寫(xiě),瀏覽器地址欄URL地址不變
a) 例如用戶(hù)訪問(wèn)www.dbspread.com,想直接跳轉(zhuǎn)到網(wǎng)站下面的某個(gè)頁(yè)面,www.dbspread.com/new.index.html如何來(lái)實(shí)現(xiàn)呢?我們可以使用Nginx Rewrite 來(lái)實(shí)現(xiàn)這個(gè)需求,具體如下:在server中加入如下語(yǔ)句即可:
效果圖如下:
rewrite ^/$ http://www.dbspread.com/new.index.html permanent;
對(duì)應(yīng)如下語(yǔ)法:
rewrite <regex> <replacement> [flag];
關(guān)鍵字 正則 替代內(nèi)容 flag標(biāo)記
正則表達(dá)式說(shuō)明:
*代表前面0或更多個(gè)字符 +代表前面1或更多個(gè)字符
?代表前面0或1個(gè)字符 ^代表字符串的開(kāi)始位置
$代表字符串結(jié)束的位置 。為通配符,代表任何字符
b)例如多個(gè)域名跳轉(zhuǎn)到同一個(gè)域名,nginx rewrite規(guī)則寫(xiě)法如下:
格式:
rewrite <regex> <replacement> [flag];
關(guān)鍵字 || 正則 || 替代內(nèi)容 || flag標(biāo)記
說(shuō)明:
比如http://www.dbspread.com/download/av123.rmvb 這個(gè)視頻下載地址被其他網(wǎng)站引用,比如在www.test.com的index.html引用download/av123.rmvb就叫盜鏈,我們要禁止這種引用就叫做防盜鏈
在nginx的nginx.conf的server里面配置如下代碼
三、Nginx 動(dòng)靜分離
Nginx動(dòng)靜分離是讓動(dòng)態(tài)網(wǎng)站里的動(dòng)態(tài)網(wǎng)頁(yè)根據(jù)一定規(guī)則把不變的資源和經(jīng)常變的資源區(qū)分開(kāi)來(lái),動(dòng)靜資源做好了拆分以后,我們就可以根據(jù)靜態(tài)資源的特點(diǎn)將其做緩存操作,這就是網(wǎng)站靜態(tài)化處理的核心思路。
1). WEB項(xiàng)目開(kāi)發(fā)時(shí)要注意,將靜態(tài)資源盡量放在一個(gè)static文件夾2). 將static靜態(tài)資源文件夾放到Nginx可以取到的位置3). 頁(yè)面要建立全局變量路徑,方便修改路徑4). 修改nginx.conf的location, 匹配靜態(tài)資源請(qǐng)求
body {
margin: 10px 20px;
text-align: center;
font-family: Arial, sans-serif;
background-color: red;
}
4.4 在nginx的nginx.conf中server節(jié)點(diǎn)新增靜態(tài)資源分離的配置
對(duì)于Nginx基礎(chǔ)配置,推薦之前的:后端實(shí)踐:Nginx日志配置(超詳細(xì))
4.5 訪問(wèn)頁(yè)面查看效果
Keepalived軟件起初是專(zhuān)為L(zhǎng)VS負(fù)載均衡軟件設(shè)計(jì)的,用來(lái)管理并監(jiān)控LVS集群系統(tǒng)中各個(gè)服務(wù)節(jié)點(diǎn)的狀態(tài),后來(lái)又加入了可以實(shí)現(xiàn)高可用的VRRP (Virtual Router Redundancy Protocol ,虛擬路由器冗余協(xié)議)功能。因此,Keepalived除了能夠管理LVS軟件外,還可以作為其他服務(wù)(例如:Nginx、Haproxy、MySQL等)的高可用解決方案軟件
管理LVS負(fù)載均衡軟件實(shí)現(xiàn)LVS集群節(jié)點(diǎn)的健康檢查作為系統(tǒng)網(wǎng)絡(luò)服務(wù)的高可用性(failover)
Keepalived高可用服務(wù)之間的故障切換轉(zhuǎn)移,是通過(guò) VRRP 來(lái)實(shí)現(xiàn)的。在 Keepalived服務(wù)正常工作時(shí),主 Master節(jié)點(diǎn)會(huì)不斷地向備節(jié)點(diǎn)發(fā)送(多播的方式)心跳消息,用以告訴備Backup節(jié)點(diǎn)自己還活著,當(dāng)主 Master節(jié)點(diǎn)發(fā)生故障時(shí),就無(wú)法發(fā)送心跳消息,備節(jié)點(diǎn)也就因此無(wú)法繼續(xù)檢測(cè)到來(lái)自主 Master節(jié)點(diǎn)的心跳了,于是調(diào)用自身的接管程序,接管主Master節(jié)點(diǎn)的 IP資源及服務(wù)。而當(dāng)主 Master節(jié)點(diǎn)恢復(fù)時(shí),備Backup節(jié)點(diǎn)又會(huì)釋放主節(jié)點(diǎn)故障時(shí)自身接管的IP資源及服務(wù),恢復(fù)到原來(lái)的備用角色。
說(shuō)明:keepalived的主從切換和redis的主從切換是不一樣的,keepalived的主節(jié)點(diǎn)掛了以后,從節(jié)點(diǎn)變?yōu)橹鞴?jié)點(diǎn),之前的主節(jié)點(diǎn)恢復(fù)以后繼續(xù)做主節(jié)點(diǎn)。redis的主節(jié)點(diǎn)掛了以后,重新恢復(fù)以后變?yōu)閺墓?jié)點(diǎn)
說(shuō)明:
虛擬ip(VIP):192.168.152.200,對(duì)外提供服務(wù)的ip,也可稱(chēng)作浮動(dòng)ip192.168.152.130:nginx + keepalived master 主192.168.152.129:nginx + keepalived backup 從192.168.152.129:tomcat-8080192.168.152.129:tomcat-8081
環(huán)境準(zhǔn)備:
centos6、jdk
虛擬ip(VIP):192.168.152.200,對(duì)外提供服務(wù)的ip,也可稱(chēng)作浮動(dòng)ip
192.168.152.130:nginx + keepalived master 主
192.168.152.129:nginx + keepalived backup 從
192.168.152.129:tomcat-8080
192.168.152.129:tomcat-8081
nginx和tomcat的環(huán)境準(zhǔn)備請(qǐng)查看我的前一篇關(guān)于nginx的文章
注:192.168.152.129(keepalived從節(jié)點(diǎn)) 與 192.168.152.130(keepalived主節(jié)點(diǎn))先安裝好nginx + keepalived
下載壓縮包:
wget www.keepalived.org/software/keepalived-1.3.5.tar.gz
解壓縮:
tar -zxvf keepalived-1.3.5.tar.gz
進(jìn)入解壓縮以后的文件目錄:
cd keepalived-1.3.5
編譯安裝:./configure --prefix=/usr/local/keepalived系統(tǒng)提示警告 *** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.yum -y install libnl libnl-devel再次執(zhí)行./configure --prefix=/usr/local/keepalived系統(tǒng)提示錯(cuò)誤 configure: error: libnfnetlink headers missingyum install -y libnfnetlink-devel再次執(zhí)行./configure --prefix=/usr/local/keepalived
make && make install
到此keepalived安裝完成,但是接下來(lái)還有最關(guān)鍵的一步,如果這一步?jīng)]有做后面啟動(dòng)keepalived的時(shí)候會(huì)報(bào)找不到配置文件的錯(cuò)誤
Configuration file '/etc/keepalived/keepalived.conf' is not a regular non-executable file
安裝完成后,進(jìn)入安裝目錄的etc目錄下,將keepalived相應(yīng)的配置文件拷貝到系統(tǒng)相應(yīng)的目錄當(dāng)中。keepalived啟動(dòng)時(shí)會(huì)從/etc/keepalived目錄下查找keepalived.conf配置文件
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
5.3 修改keepalived從節(jié)點(diǎn)192.168.152.129的/etc/keepalived/keepalived.conf配置文件
5.4 檢查nginx是否啟動(dòng)的shell腳本
/usr/local/src/check_nginx_pid.sh
#!/bin/bash
#檢測(cè)nginx是否啟動(dòng)了
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then #如果nginx沒(méi)有啟動(dòng)就啟動(dòng)nginx
/usr/local/nginx/sbin/nginx #重啟nginx
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重啟失敗,則停掉keepalived服務(wù),進(jìn)行VIP轉(zhuǎn)移
killall keepalived
fi
fi
5.5 192.168.152.130(keepalived主節(jié)點(diǎn))和 192.168.152.129(keepalived從節(jié)點(diǎn))的nginx的配置文件nginx.conf
user root root; #使用什么用戶(hù)啟動(dòng)NGINX 在運(yùn)行時(shí)使用哪個(gè)用戶(hù)哪個(gè)組
worker_processes 4; #啟動(dòng)進(jìn)程數(shù),一般是1或8個(gè),根據(jù)你的電腦CPU數(shù),一般8個(gè)
worker_cpu_affinity 00000001 00000010 00000100 00001000; #CPU邏輯數(shù)——把每個(gè)進(jìn)程分別綁在CPU上面,為每個(gè)進(jìn)程分配一個(gè)CPU
#pid /usr/local/nginx/logs/nginx.pid
worker_rlimit_nofile 102400; #一個(gè)進(jìn)程打開(kāi)的最大文件數(shù)目,與NGINX并發(fā)連接有關(guān)系
#工作模式及連接數(shù)上限
events
{
use epoll; #多路復(fù)用IO 基于LINUX2.6以上內(nèi)核,可以大大提高NGINX的性能 uname -a查看內(nèi)核版本號(hào)
worker_connections 102400; #單個(gè)worker process最大連接數(shù),其中NGINX最大連接數(shù)=連接數(shù)*進(jìn)程數(shù),一般1GB內(nèi)存的機(jī)器上可以打開(kāi)的最大數(shù)大約是10萬(wàn)左右
multi_accept on; #盡可能多的接受請(qǐng)求,默認(rèn)是關(guān)閉狀態(tài)
}
#處理http請(qǐng)求的一個(gè)應(yīng)用配置段
http
{
#引用mime.types,這個(gè)類(lèi)型定義了很多,當(dāng)web服務(wù)器收到靜態(tài)的資源文件請(qǐng)求時(shí),依據(jù)請(qǐng)求文件的后綴名在服務(wù)器的MIME配置文件中找到對(duì)應(yīng)的MIME #Type,根據(jù)MIMETYPE設(shè)置并response響應(yīng)類(lèi)型(Content-type)
include mime.types;
default_type application/octet-stream; #定義的數(shù)據(jù)流,有的時(shí)候默認(rèn)類(lèi)型可以指定為text,這跟我們的網(wǎng)頁(yè)發(fā)布還是資源下載是有關(guān)系的
fastcgi_intercept_errors on; #表示接收f(shuō)astcgi輸出的http 1.0 response code
charset utf-8;
server_names_hash_bucket_size 128; #保存服務(wù)器名字的hash表
#用來(lái)緩存請(qǐng)求頭信息的,容量4K,如果header頭信息請(qǐng)求超過(guò)了,nginx會(huì)直接返回400錯(cuò)誤,先根據(jù)client_header_buffer_size配置的值分配一個(gè)buffer,如果##分配的buffer無(wú)法容納request_line/request_header,那么就會(huì)##再次根據(jù)large_client_header_buffers配置的參數(shù)分配large_buffer,如果large_buffer還是無(wú)#法容納,那么就會(huì)返回414(處理request_line)/400(處理request_header)錯(cuò)誤。
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m; #允許客戶(hù)端請(qǐng)求的最大單文件字節(jié)數(shù) 上傳文件時(shí)根據(jù)需求設(shè)置這個(gè)參數(shù)
#指定NGINX是否調(diào)用這個(gè)函數(shù)來(lái)輸出文件,對(duì)于普通的文件我們必須設(shè)置為ON,如果NGINX專(zhuān)門(mén)做為一個(gè)下載端的話(huà)可以關(guān)掉,好處是降低磁盤(pán)與網(wǎng)絡(luò)的IO處理數(shù)及#系統(tǒng)的UPTIME
sendfile on;
#autoindex on;開(kāi)啟目錄列表訪問(wèn),適合下載服務(wù)器
tcp_nopush on; #防止網(wǎng)絡(luò)阻塞
#非常重要,根據(jù)實(shí)際情況設(shè)置值,超時(shí)時(shí)間,客戶(hù)端到服務(wù)端的連接持續(xù)有效時(shí)間,60秒內(nèi)可避免重新建立連接,時(shí)間也不能設(shè)太長(zhǎng),太長(zhǎng)的話(huà),若請(qǐng)求數(shù)10000##,都占用連接會(huì)把服務(wù)托死
keepalive_timeout 60;
tcp_nodelay on; #提高數(shù)據(jù)的實(shí)時(shí)響應(yīng)性
client_body_buffer_size 512k; #緩沖區(qū)代理緩沖用戶(hù)端請(qǐng)求的最大字節(jié)數(shù)(請(qǐng)求多)
proxy_connect_timeout 5; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí))
proxy_read_timeout 60; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí))
proxy_send_timeout 5; #后端服務(wù)器數(shù)據(jù)回傳時(shí)間(代理發(fā)送超時(shí))
proxy_buffer_size 16k; #設(shè)置代理服務(wù)器(nginx)保存用戶(hù)頭信息的緩沖區(qū)大小
proxy_buffers 4 64k; #proxy_buffers緩沖區(qū),網(wǎng)頁(yè)平均在32k以下的話(huà),這樣設(shè)置
proxy_busy_buffers_size 128k; #高負(fù)荷下緩沖大小
proxy_temp_file_write_size 128k; #設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳
gzip on; #NGINX可以壓縮靜態(tài)資源,比如我的靜態(tài)資源有10M,壓縮后只有2M,那么瀏覽器下載的就少了
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2; #壓縮級(jí)別大小,最小1,最大9.值越小,壓縮后比例越小,CPU處理更快,為1時(shí),原10M壓縮完后8M,但設(shè)為9時(shí),壓縮完可能只有2M了。一般設(shè)置為2
gzip_types text/plain application/x-javascript text/css application/xml; #壓縮類(lèi)型:text,js css xml 都會(huì)被壓縮
gzip_vary on; #作用是在http響應(yīng)中增加一行目的是改變反向代理服務(wù)器的緩存策略
#日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #ip 遠(yuǎn)程用戶(hù) 當(dāng)?shù)貢r(shí)間 請(qǐng)求URL
'$status $body_bytes_sent "$http_referer" ' #狀態(tài) 發(fā)送的大小 響應(yīng)的頭
'"$http_user_agent" $request_time'; #客戶(hù)端使用的瀏覽器 頁(yè)面響應(yīng)的時(shí)間
#動(dòng)態(tài)轉(zhuǎn)發(fā)
upstream web1 {
#每個(gè)請(qǐng)求按訪問(wèn)ip的hash結(jié)果分配,這樣每個(gè)訪客固定訪問(wèn)一個(gè)后端服務(wù)器,可以解決session的問(wèn)題。配置了ip_hash就沒(méi)有負(fù)載均衡的效果了,每次訪問(wèn)的都是同一個(gè)tomcat
#ip_hash;
#轉(zhuǎn)發(fā)的后端的tomcat服務(wù)器,weight表示轉(zhuǎn)發(fā)的權(quán)重,越大轉(zhuǎn)發(fā)的次數(shù)越多,機(jī)器性能不一樣配置的weight值不一樣
server 192.168.152.129:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.152.129:8081 weight=1 max_fails=2 fail_timeout=30s;
}
upstream web2 {
server 192.168.152.129:8090 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.152.129:8091 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80; #監(jiān)聽(tīng)80端口
server_name www.dbspread.com; #域名
#rewrite規(guī)則
index index.jsp index.html index.htm;
root /usr/local/nginx/html; #定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置
#重定向
if ($host != 'www.dbspread.com' ){
rewrite ^/(.*)$ http://www.dbspread.com/$1 permanent;
}
#防盜鏈
location ~* \.(rmvb|jpg|png|swf|flv)$ { #rmvb|jpg|png|swf|flv表示對(duì)rmvb|jpg|png|swf|flv后綴的文件實(shí)行防盜鏈
valid_referers none blocked www.dbspread.com; #表示對(duì)www.dbspread.com此域名開(kāi)通白名單,比如在www.test.com的index.html引用download/av123.rmvb,無(wú)效
root html/b;
if ($invalid_referer) { #如果請(qǐng)求不是從www.dbspread.com白名單發(fā)出來(lái)的請(qǐng)求,直接重定向到403.html這個(gè)頁(yè)面或者返回403
#rewrite ^/ http://www.dbspread.com/403.html;
return 403;
}
}
#監(jiān)聽(tīng)完成以后通過(guò)斜桿(/)攔截請(qǐng)求轉(zhuǎn)發(fā)到后端的tomcat服務(wù)器
location /
{
#如果后端的服務(wù)器返回502、504、執(zhí)行超時(shí)等錯(cuò)誤,自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)故障轉(zhuǎn)移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host; #獲取客戶(hù)端的主機(jī)名存到變量Host里面,從而讓tomcat取到客戶(hù)端機(jī)器的信息
proxy_set_header X-Real-IP $remote_addr; #獲取客戶(hù)端的主機(jī)名存到變量X-Real-IP里面,從而讓tomcat取到客戶(hù)端機(jī)器的信息
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#rewrite ^/$ http://www.dbspread.com/new.index.html permanent;#用戶(hù)訪問(wèn)www.dbspread.com,想直接跳轉(zhuǎn)到網(wǎng)站下面的某個(gè)頁(yè)面:www.dbspread.com/new.index.html
proxy_pass http://web1; #跳轉(zhuǎn)到對(duì)應(yīng)的應(yīng)用web1
}
# location ~ .*\.(php|jsp|cgi|shtml)?$ #動(dòng)態(tài)分離 ~匹配 以.*結(jié)尾(以PHP JSP結(jié)尾走這段)
# {
# 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_pass http://jvm_web2;
# }
#靜態(tài)分離 ~匹配 以.*結(jié)尾(以html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css結(jié)尾走這段),當(dāng)然不是越久越好,如果有10000個(gè)用戶(hù)在線,都保存幾個(gè)月,系統(tǒng)托跨
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /var/local/static; #靜態(tài)資源存放在nginx的安裝機(jī)器上
#proxy_pass http://www.static.com; #靜態(tài)資源也可存放在遠(yuǎn)程服務(wù)器上
expires 30d;
}
#日志級(jí)別有[debug|info|notice|warn|error|crit] error_log 級(jí)別分為 debug, info, notice, warn, error, crit 默認(rèn)為crit, 生產(chǎn)環(huán)境用error
#crit 記錄的日志最少,而debug記錄的日志最多
access_log /usr/local/logs/web2/access.log main;
error_log /usr/local/logs/web2/error.log crit;
}
}
到這一步環(huán)境準(zhǔn)備已完成,相關(guān)的配置也修改完成,下面我們來(lái)查看效果
5.6 配置hosts域名映射
192.168.152.200 www.dbspread.com
注意:這里192.168.152.200 是keepalived里面virtual_ipaddress配置的虛擬ip
virtual_ipaddress {
192.168.152.200 # 定義虛擬ip(VIP),可多設(shè),每行一個(gè)
}
到這一步環(huán)境準(zhǔn)備已完成,相關(guān)的配置也修改完成,下面我們來(lái)查看效果
5.7 分別啟動(dòng)192.168.152.129的兩個(gè)tomcat
5.8 分別啟動(dòng)192.168.152.130(keepalived主節(jié)點(diǎn))和
192.168.152.129(keepalived從節(jié)點(diǎn))的keepalived的
啟動(dòng)命令:
/usr/local/keepalived/sbin/keepalived
可以看到keepalived和nginx都啟動(dòng)了
在瀏覽器輸入www.dpspread.com域名訪問(wèn)
可以看到從節(jié)點(diǎn)變?yōu)橹鞴?jié)點(diǎn)了
在瀏覽器輸入地址www.dpspread.com訪問(wèn),可以看到訪問(wèn)正常
可以看到主節(jié)點(diǎn)重新啟動(dòng)以后變?yōu)橹鞴?jié)點(diǎn)了
之前變?yōu)橹鞴?jié)點(diǎn)的從節(jié)點(diǎn)又變回從節(jié)點(diǎn)了
ginx 是一個(gè)高性能的反向代理服務(wù)器,也是一個(gè)非常流行的負(fù)載均衡器和 HTTP 緩存。其輕量級(jí)的設(shè)計(jì)和高并發(fā)處理能力使得它廣泛應(yīng)用于各種 Web 服務(wù)中。在使用 Nginx 作為反向代理服務(wù)器時(shí),一個(gè)常見(jiàn)的問(wèn)題是如何在代理轉(zhuǎn)發(fā)過(guò)程中傳遞客戶(hù)端的真實(shí) IP 地址。默認(rèn)情況下,Nginx 會(huì)將客戶(hù)端的 IP 地址替換為代理服務(wù)器的 IP 地址,這可能會(huì)在某些情況下引發(fā)問(wèn)題,比如日志記錄、訪問(wèn)控制和地理位置追蹤等。
傳遞真實(shí) IP 地址的需求主要有以下幾個(gè)原因:
最常用的方法是通過(guò) X-Forwarded-For HTTP 頭傳遞客戶(hù)端的真實(shí) IP 地址。Nginx 可以在代理轉(zhuǎn)發(fā)請(qǐng)求時(shí)添加這個(gè)頭,以便后端服務(wù)器能夠獲取到真實(shí)的客戶(hù)端 IP 地址。
在 Nginx 配置文件中,可以使用以下指令來(lái)設(shè)置 X-Forwarded-For 頭:
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend_server;
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 $scheme;
}
}
}
在上述配置中:
后端服務(wù)器需要正確解析 X-Forwarded-For 頭以獲取客戶(hù)端的真實(shí) IP 地址。例如,在 Apache 中,可以使用 mod_remoteip 模塊:
<IfModule mod_remoteip.c>
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 10.0.0.0/8
</IfModule>
在以上配置中:
Nginx 提供了一個(gè) ngx_http_realip_module 模塊,用于處理 X-Forwarded-For 頭并將其作為客戶(hù)端的真實(shí) IP 地址。
首先,確保 Nginx 已經(jīng)編譯并啟用了 ngx_http_realip_module 模塊。可以通過(guò)以下命令檢查:
nginx -V 2>&1 | grep -o with-http_realip_module
如果輸出結(jié)果中包含 with-http_realip_module,則表示該模塊已啟用。
在 Nginx 配置文件中,可以使用以下配置來(lái)啟用 real_ip 模塊:
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
real_ip_header X-Forwarded-For;
set_real_ip_from 10.0.0.0/8;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend_server;
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 $scheme;
}
}
}
在以上配置中:
假設(shè)有一個(gè)前端 Nginx 服務(wù)器和一個(gè)后端應(yīng)用服務(wù)器,前端服務(wù)器的 IP 地址為 192.168.1.1,后端服務(wù)器的 IP 地址為 192.168.1.2。
在前端 Nginx 服務(wù)器上,可以使用以下配置:
server {
listen 80;
server_name frontend.example.com;
location / {
proxy_pass http://192.168.1.2;
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 $scheme;
}
}
在后端應(yīng)用服務(wù)器上,可以使用以下配置:
server {
listen 80;
server_name backend.example.com;
real_ip_header X-Forwarded-For;
set_real_ip_from 192.168.1.1;
location / {
root /var/www/html;
index index.html index.htm;
}
}
配置完成后,可以通過(guò)以下步驟驗(yàn)證真實(shí) IP 地址的傳遞是否正確:
例如,可以使用 curl 命令發(fā)送請(qǐng)求:
curl -I http://frontend.example.com
然后,在后端應(yīng)用服務(wù)器的日志中檢查請(qǐng)求的 IP 地址:
tail -f /var/log/nginx/access.log
日志中應(yīng)顯示客戶(hù)端的真實(shí) IP 地址,而不是前端 Nginx 服務(wù)器的 IP 地址。
解決方法:確保在后端服務(wù)器的 Nginx 配置中正確設(shè)置了 real_ip_header 和 set_real_ip_from 指令,并且前端服務(wù)器已正確設(shè)置 X-Forwarded-For 頭。
解決方法:在后端服務(wù)器的配置中,確保 real_ip_recursive 指令已啟用,以處理包含多個(gè) IP 地址的 X-Forwarded-For 頭。
http {
real_ip_header X-Forwarded-For;
set_real_ip_from 192.168.1.1;
real_ip_recursive on;
}
在使用 Nginx 作為反向代理服務(wù)器時(shí),傳遞客戶(hù)端的真實(shí) IP 地址對(duì)于日志記錄、訪問(wèn)控制和地理位置追蹤等應(yīng)用至關(guān)重要。通過(guò)使用 X-Forwarded-For 頭和 ngx_http_realip_module 模塊,可以有效地實(shí)現(xiàn)這一需求。正確配置后端服務(wù)器以解析這些頭信息,將確保能夠正確獲取到客戶(hù)端的真實(shí) IP 地址,從而提升系統(tǒng)的可靠性和準(zhǔn)確性。
信息爆炸的時(shí)代,每個(gè)人每天都在使用各種聯(lián)網(wǎng)應(yīng)用,也在不停的產(chǎn)生各種屬于自己隱私的數(shù)據(jù)(文本、照片、音視頻等)。這些數(shù)據(jù)如果放置在互聯(lián)網(wǎng)的網(wǎng)盤(pán)上,似乎不夠安全,不夠保險(xiǎn)。同時(shí),免費(fèi)的網(wǎng)盤(pán)往往伴隨著限速、限空間,無(wú)緣無(wú)故“和諧”掉一些內(nèi)容,使用起來(lái)非常不順手。于是,搭建自己的家庭服務(wù)器就變得非常有必要。
參數(shù)項(xiàng) | 參數(shù)內(nèi)容 | 選擇理由 |
CPU | Intel(R) Celeron(R) J4125 | X86架構(gòu),64位4核CPU,性能足夠、功耗低、可無(wú)風(fēng)扇穩(wěn)定運(yùn)行 |
內(nèi)存 | 16G DDR4 | 16G足夠使用 |
硬盤(pán) | 128G SSD + 2T SSD/SATA | SSD安裝系統(tǒng)和存放應(yīng)用,2T SSD/SATA存放網(wǎng)盤(pán)數(shù)據(jù)(省錢(qián)一點(diǎn),數(shù)據(jù)盤(pán)可以利舊或者買(mǎi)2.5寸機(jī)械盤(pán)) |
接口 | 2 X 千兆網(wǎng)口+WIFI(2.4G+5G) + 若干usb口 | 1個(gè)網(wǎng)口作為WAN口,另外1個(gè)網(wǎng)口和WIFI橋接作為L(zhǎng)AN口。USB口還可以外掛閑置的移動(dòng)硬盤(pán)。 |
參數(shù)項(xiàng) | 參數(shù)內(nèi)容 | 實(shí)現(xiàn)功能 | 備注 |
OS | ubuntu Server 22.04 LTS+cockpit | 操作系統(tǒng)+web管理 | 最小化安裝,22.04是當(dāng)前最新版本 |
docker | docker-ce、docker-compose | 容器平臺(tái) | 容器需要使用的軟件 |
影音系統(tǒng) | jellyfin | 影音播放 | 容器安裝 |
知識(shí)庫(kù) | dokuwiki | wiki形式記錄知識(shí) | 容器安裝 |
網(wǎng)盤(pán) | cloudreve+aria2 | 通過(guò)web、webdav使用硬盤(pán)空間,aria2實(shí)現(xiàn)離線下載 | 容器安裝 |
密碼庫(kù) | vaultwarden | 記錄web密碼,并實(shí)現(xiàn)自動(dòng)填充 | 容器安裝 |
apt update
apt upgrade
包括編輯器vim、網(wǎng)絡(luò)工具net-tools、ping命令支持iputils-ping、nslookup支持dnsutils、定時(shí)任務(wù)cron、解壓縮unzip、硬盤(pán)檢測(cè)工具smartmontools、防火墻firewalld。
apt update
apt upgrade
apt install vim net-tools iputils-ping dnsutils cron unzip smartmontools firewalld
timedatectl set-timezone Asia/Shanghai
配置網(wǎng)絡(luò),增加網(wǎng)橋br0,目的是給后面軟路由op虛擬機(jī)橋接使用。
vi /etc/netplan/00-installer-config.yaml
樣本配置文件如下:
ethernets:
enp1s0:
dhcp4: false
optional: true
enp2s0:
optional: true
bridges:
br0:
interfaces:
- enp1s0
addresses:
- 192.168.2.18/24 #根據(jù)個(gè)人喜歡選擇自己的IP和網(wǎng)段
nameservers:
addresses:
- 8.8.8.8
search: []
routes:
- to: default
via: 192.168.2.1
parameters:
stp: true
# 打開(kāi)STP協(xié)議,防止環(huán)路;
version: 2
renderer: networkd
應(yīng)用配置
netplan apply
這里使用cockpit管理面板來(lái)實(shí)現(xiàn)遠(yuǎn)程管理,改軟件有不少使用組件可以根據(jù)自己需要安裝,這里安裝了虛擬機(jī)管理組件 cockpit-machine。
apt install cockpit cockpit-machine
安裝必要的證書(shū)并允許 apt 包管理器使用以下命令通過(guò) HTTPS 使用存儲(chǔ)庫(kù):
apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release
添加 Docker 的官方 GPG 密鑰:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
添加 Docker 官方庫(kù):
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
更新 Ubuntu 源列表:
apt update
安裝docker-ce、docker-compose
apt install docker-ce docker-compose
docker默認(rèn)的橋接網(wǎng)絡(luò)沒(méi)有開(kāi)放ipv6,這里自己創(chuàng)建一個(gè)支持ipv6的容器網(wǎng)絡(luò)
docker network create --ipv6 dnet6
在自己喜歡的目錄下(比如:/me)創(chuàng)建compose文件來(lái)實(shí)現(xiàn)各種功能
version: "3"
#--------------------全局設(shè)置----------------------#
# using "docker network create --ipv6 dnet6" command to create this network first.
networks:
default:
external:
name: dnet6
volumes:
temp_data:
driver: local
driver_opts:
type: none
device: /data/cloudreve/data
o: bind
#--------------------服務(wù)開(kāi)始--------------------#
services:
#---------nginx反向代理(本人僅用來(lái)代理密碼庫(kù))--------#
nginx:
image: docker.io/library/nginx:latest
container_name: nginx
ports:
- 8000:80
- "4430:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf #需要先在主機(jī)目錄下創(chuàng)建nginx.conf配置文件
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/html:/usr/share/nginx/html
- ./nginx/log:/var/log/nginx
environment:
- TZ=Asia/Shanghai
restart: always
#--------------------知識(shí)庫(kù)--------------------#
dokuwiki:
image: docker.io/linuxserver/dokuwiki:latest
container_name: dokuwiki
ports:
- 8001:80
- "8043:443"
environment:
- PUID=1000
- PGID=1000
- TZ=Asia/Shanghai
volumes:
- ./dokuwiki:/config
restart: always
#-------------------密碼庫(kù)-------------------#
vaultwarden:
image: docker.io/vaultwarden/server:latest
container_name: vaultwarden
ports:
- "8002:80"
environment:
- WEBSOCKET_ENABLED=true
- LOG_FILE=/log/bitwarden.log
- ADMIN_TOKEN=your-passwrod #填寫(xiě)你自己的密碼
#- ROCKET_TLS={certs="/ssl/server.crt.pem",key="/ssl/server.key.pem"} #有反向代理無(wú)需啟用
- TZ=Asia/Shanghai
volumes:
- ./vaultwarden/ssl:/ssl
- ./vaultwarden/data:/data
- ./vaultwarden/log:/log
restart: always
#--------------------私家網(wǎng)盤(pán)--------------------#
cloudreve:
container_name: cloudreve
image: docker.io/cloudreve/cloudreve:latest
restart: always
ports:
- "5212:443"
volumes:
- temp_data:/data
- ./cloudreve/uploads:/cloudreve/uploads
- ./cloudreve/conf.ini:/cloudreve/conf.ini
- ./cloudreve/cloudreve.db:/cloudreve/cloudreve.db
- ./cloudreve/avatar:/cloudreve/avatar
- ./cloudreve/ssl:/cloudreve/ssl #存放自己生成的SSL證書(shū)
depends_on:
- aria2
#------------------離線下載----------------------#
aria2:
container_name: aria2
image: docker.io/p3terx/aria2-pro
restart: always
ports:
- "6800:6800"
environment:
- RPC_SECRET=your-password #設(shè)置自己的通訊密碼
- RPC_PORT=6800
- TZ=Asia/Shanghai
volumes:
- ./cloudreve/aria2/config:/config
- temp_data:/data
logging:
driver: k8s-file
options:
max-size: 1m
#------------私家影音平臺(tái)-------------#
jellyfin:
image: lscr.io/linuxserver/jellyfin:latest
container_name: jellyfin
#runtime: runc
ports:
- "8096:8096"
- "8920:8920" #optional https port 需要配置SSL證書(shū)
#- 7359:7359/udp #optional auto lan discover
#- 1900:1900/udp #optional dlna port
volumes:
- ./jellyfin/config:/config
- ./jellyfin/scripts:/custom-cont-init.d
- ./media/path1:/data/media:ro #設(shè)置自己影音文件存放位置
- ./media/path2:/data/media2:ro #設(shè)置自己影音文件存放位置
devices:
- /dev/dri:/dev/dri #VAAPI/NVDEC/NVENC render nodes
restart: always
environment:
- PUID=0 #1000 means docker user;0 means root user;
- PGID=0 #1000 means docker user;0 means root user;
- TZ=Asia/Shanghai
#- DOCKER_MODS=linuxserver/mods:jellyfin-opencl-intel #optional - auto download and install chinese font for create docker first time
#- JELLYFIN_PublishedServerUrl=http://192.168.8.18/ #optional - may be necessary for docker healthcheck to pass if running in host network mode
#extra_hosts:
# - "host.docker.internal:host-gateway"
#---------------服務(wù)結(jié)束--------------------#
進(jìn)入compose.yml所在目錄,并執(zhí)行以下命令拉取鏡像以及創(chuàng)建容器。
docker-compose up -d
備注:
1:喜歡用podman的人,這里可以用podman代替docker。cockpit還有專(zhuān)門(mén)的podman支持的插件cockpit-podman,可以實(shí)現(xiàn)web圖形化管理podman容器。
2:以上開(kāi)源軟件基本都能開(kāi)啟HTTPS加密訪問(wèn),但需要自行配置證書(shū);
修改nginx配置文件:nginx目錄/conf.d/default.conf:
server {
listen 443 ssl;
listen [::]:443 ssl;
#http2 on;
server_name domain-name; #配置自己用到的域名
# 根據(jù)需要指定 SSL 配置
ssl_certificate /etc/nginx/conf.d/server.crt.pem;
ssl_certificate_key /etc/nginx/conf.d/server.key.pem;
ssl_trusted_certificate /etc/nginx/conf.d/ca.crt.pem;
client_max_body_size 525M;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
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 $scheme;
proxy_pass http://serverip:port; #填寫(xiě)真實(shí)服務(wù)器的ip和端口
}
由于ipv4地址資源非常緊張,目前一般的寬帶網(wǎng)絡(luò)都沒(méi)有ipv4公網(wǎng)地址了。但好在ipv6非常充裕,大部分人都可以獲得可公網(wǎng)訪問(wèn)的ipv6地址。于是我們就可以使用ddns+ipv6來(lái)實(shí)現(xiàn)遠(yuǎn)程訪問(wèn)。
crontab -e
按i并在文末加上下文:
*/5 * * * * /me/ddns/dynamic-url-perl.pl
輸入:wq保存退出。這樣就實(shí)現(xiàn)了每5分鐘更新ip的動(dòng)態(tài)域名了
注意點(diǎn):由于該perl腳本依賴(lài)http::tiny模塊,所以需要通過(guò)下面命令安裝libhttp-tiny-perl。
apt install libhttp-tiny-perl
由于我的機(jī)器是intel CPU,顧需要開(kāi)啟intel-vt和intel_iommu特性。
在openwrt官方網(wǎng)站的鏡像選擇器中選擇下載。
這里根據(jù)我cpu架構(gòu)選擇了x86/64,軟件選擇最新的發(fā)行版本,下載得到openwrt-22.03.5-x86-64-generic-ext4-combined.img.gz。
通過(guò)scp直接上傳,命令如下:
scp d:\download\openwrt-22.03.5-x86-64-generic-ext4-combined.img.gz user@serverip:/data/
gzip -d openwrt-22.03.5-x86-64-generic-ext4-combined.img.gz
解壓后得到openwrt-22.03.5-x86-64-generic-ext4-combined.img文件。
vi /etc/config/network
config interface 'lan'
option device 'br-lan'
option protocol 'static'
option netmask '255.255.255.0'
option ipaddr '192.168.1.1' #按需替換ip
option ip6assign '60'
option gateway '192.168.1.254' #按需替換網(wǎng)關(guān)
list dns '8.8.8.8'
這里的IP可以臨時(shí)改成咋們局域網(wǎng)的一個(gè)可用IP地址,并增加以下配置以實(shí)現(xiàn)單臂路由上網(wǎng)。
uci set zerotier.openwrt_network=zerotier
uci add_list zerotier.openwrt_network.join='你的網(wǎng)絡(luò)ID'
uci set zerotier.openwrt_network.enabled='1'
uci commit zerotier
受篇幅影響,以上記錄盡量簡(jiǎn)化(簡(jiǎn)化自本人自家搭建筆錄而來(lái)),但保留了所有主要步驟,完全可以實(shí)現(xiàn)私家服務(wù)器、vip網(wǎng)盤(pán)、vip影音系統(tǒng)功能。當(dāng)然結(jié)合容器安裝更多的實(shí)用開(kāi)源軟件,可以實(shí)現(xiàn)更多的功能,只需要你的小主機(jī)依然有資源。
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。