Nginx配置方向代理及目錄白名單配置教程
1、Nginx反向代理配置
nginx 默認(rèn)的配置文件是nginx.conf,進(jìn)入nginx配置文件目錄下打開配置文件

剛安裝完的nginx.conf配置內(nèi)容如下:
#user nobody;
worker_processes 1; #配置工作進(jìn)程數(shù)目,根據(jù)硬件調(diào)整,通常等于CPU數(shù)量或2倍于CPU數(shù)量
#error_log logs/error.log; #制定日志路徑,級(jí)別。這個(gè)設(shè)置可以放入全局塊,http塊,server塊,級(jí)別以此為:debug|info|notice|warn|error|crit|alert|emerg
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #指定nginx進(jìn)程運(yùn)行文件存放地址
events {
worker_connections 1024; #最大連接數(shù),默認(rèn)為512
}
http {
include mime.types; #文件擴(kuò)展名與文件類型映射表
default_type application/octet-stream; #默認(rèn)文件類型,默認(rèn)為text/plain
#配置日志格式
#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; #配置access.log日志及存放路徑,并且使用上面定義的main日志格式
sendfile on; #允許sendfile方式傳輸文件,默認(rèn)為off,可以在http塊,server塊,location塊。
#tcp_nopush on; #防止網(wǎng)絡(luò)阻塞
#keepalive_timeout 0;
keepalive_timeout 65; #連接超時(shí)時(shí)間,默認(rèn)為75s,可以在http,server,location塊。
#gzip on; #是否開啟gzip壓縮輸出
#以下是配置跨域(Nginx設(shè)置響應(yīng)頭信息給瀏覽器)
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type;
server { #服務(wù),可以有多個(gè)server,對(duì)應(yīng)多個(gè)服務(wù)
listen 80; #監(jiān)聽端口 ,此項(xiàng)如果不配置則默認(rèn)80端口
server_name localhost; #配置服務(wù)名
#charset koi8-r; #配置字符集
#access_log logs/host.access.log main; #配置本虛擬主機(jī)的訪問日志
#默認(rèn)的匹配斜杠/的請(qǐng)求,當(dāng)訪問路徑有斜杠,會(huì)被該location匹配并且進(jìn)行處理
location / {
root html; #root是配置服務(wù)器的默認(rèn)網(wǎng)站根目錄位置,默認(rèn)為nginx安裝主目錄下的html目錄
index index.html index.htm; #配置首頁(yè)文件的名稱
}
#error_page 404 /404.html; #配置404頁(yè)面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #配置50x錯(cuò)誤頁(yè)面
location = /50x.html {
root html;
}
#PHP 腳本請(qǐng)求全部轉(zhuǎn)發(fā)到Apache處理
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#PHP 腳本請(qǐng)求全部轉(zhuǎn)發(fā)到FastCGI處理
# 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;
#}
#禁止訪問.htaccess 文件
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#配置另外一臺(tái)虛擬主機(jī)
# 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服務(wù)
# 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 文件結(jié)構(gòu)說明:
Nginx的核心配置文件主要由三個(gè)部分構(gòu)成
1)基本配置(全局模塊)
2)events配置
3)http配置
http{}模塊包含server{}塊和location{}塊;而server{}塊又包含location{}塊。
1、全局塊:
- 配置影響nginx全局的指令。
- 一般有運(yùn)行nginx服務(wù)器的用戶組,nginx進(jìn)程pid存放路徑,日志存放路徑等。
2、events塊:
- 配置影響nginx服務(wù)器或與用戶的網(wǎng)絡(luò)連接。
- 有每個(gè)進(jìn)程的最大連接數(shù),選取哪種事件驅(qū)動(dòng)模型處理連接請(qǐng)求,是否允許同時(shí)接受多個(gè)網(wǎng)路連接,開啟多個(gè)網(wǎng)絡(luò)連接序列化等。
3、http塊:
- 直接隸屬于http{}塊內(nèi)的配置項(xiàng)稱為main配置項(xiàng)
- 可以嵌套多個(gè)server,配置代理,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置。如連接超時(shí)時(shí)間,單連接請(qǐng)求數(shù)等。
4、server塊:
- 直接隸屬于server{}塊內(nèi)的配置項(xiàng)稱為srv配置項(xiàng)
- 配置虛擬主機(jī)的相關(guān)參數(shù),一個(gè)http中可以有多個(gè)server,可以有多個(gè)server,對(duì)應(yīng)多個(gè)服務(wù)。
5、location塊:
- 直接隸屬于location{}塊內(nèi)的配置項(xiàng)稱為loc配置項(xiàng)
- 配置請(qǐng)求的路由,以及各種頁(yè)面的處理情況。
配置例子:
#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 {undefined
worker_connections 1024; #最大連接數(shù),默認(rèn)為512
}
http {undefined
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; #連接超時(shí)時(shí)間,默認(rèn)為75s,可以在http,server,location塊。
#gzip on;
//以下是負(fù)載均衡配置包含多個(gè)配置項(xiàng),主要有backup,weight,down,max_fails,fail_timeout.. proxy_next_upstream,
如:upstream:www.aaaaa.com{}指定了三個(gè)服務(wù)器,其中8082端口服務(wù)器因?yàn)閹в衎ackup標(biāo)識(shí),作為備用服務(wù)器存在,也就是說當(dāng)8081服務(wù)器正常時(shí),8082服務(wù)器是不參與工作的,僅當(dāng)8081服務(wù)器遇到了proxy_next_upstream指定的錯(cuò)誤類型( error | timeout | http_502 | http_503 | http_504),nginx才會(huì)切換到備用服務(wù)器。
weight: #指定了服務(wù)器的權(quán)重, 其中8080服務(wù)器的流量將會(huì)是8081服務(wù)器的2倍。
max_fails: #允許請(qǐng)求失敗的次數(shù),默認(rèn)為1。當(dāng)超過最大次數(shù)時(shí),返回proxy_next_upstream 模塊定義的錯(cuò)誤
fail_timeout:# 默認(rèn)值10s,代表超時(shí)時(shí)間。
down: #表示當(dāng)前的server暫時(shí)不參與負(fù)載
ip_hash: #每個(gè)請(qǐng)求按訪問ip的hash結(jié)果分配,這樣每個(gè)訪客固定訪問一個(gè)后端服務(wù)器,可以解決session不能跨服務(wù)器的問題,如果后端服務(wù)器down掉,要手工down掉。
upstream www.aaaaa.com {
server 127.0.0.1:8080; weight=2 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8082 weight=1 max_fails=3 fail_timeout=30s backup;
}
upstream www.bbbbb.net {
server 127.0.0.1:9999;
}
upstream www.ccccc.cn {
server 127.0.0.1:8888;
}
upstream weixin.ddddd.com {
server 127.0.0.1:8080;
}
upstream weixin.eeeee.com {
server 127.0.0.1:8899;
}
upstream 105.26.224.50 {
server 127.0.0.1:8899;
}
upstream 105.26.224.52 {
server 127.0.0.1:8899;
}
upstream demo.wly.fffff.com {
server 127.0.0.1:7777;
}
//以下是多站點(diǎn)多應(yīng)用配置,各自有不同的域名,www.aaaaa.com將被自動(dòng)代理到http://www.aaabb.com:8080服務(wù)器上, 而www.bbbbb.net:8081則被自動(dòng)代理到http://www.bbbcc.net:8082服務(wù)器上。
域名情況下,proxy_name,upstream
server {
listen 80;
server_name www.aaaaa.com;
charset utf-8; #編碼格式
#location是nginx配置文件中的一個(gè)配置項(xiàng), 當(dāng)需要在一個(gè)域名下面部署多個(gè)不同服務(wù)器,可使用該方案,配置如下
# http://www.aaaaa.com:80/mail/ 下的請(qǐng)求轉(zhuǎn)發(fā)到 http://www.wly.com:80/protmail/
location /mail/ {
index index.html index.jsp;
proxy_pass http://www.wly.com:80/protmail/; #指定要代理的服務(wù)器
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
# http://www.aaaaa.com:80/mail/ 下的請(qǐng)求轉(zhuǎn)發(fā)到 http://www.wly.com:80/protmail/mail/
location /com/ {
index index.html index.jsp;
proxy_pass http://www.wly.com:80/protmail/mail/;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
# 將其它所有請(qǐng)求轉(zhuǎn)發(fā)到 http://www.aaabb.com:8080
location / {
index index.html index.jsp;
proxy_pass http://www.aaabb.com:8080;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.bbbbb.net;
location / {
index index.html index.jsp;
proxy_pass http://www.bbbcc.net;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.ccccc.cn;
location / {
index index.html index.jsp;
proxy_pass http://www.ccccc.cn:8081;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8082;
server_name weixin.ddddd.com;
location / {
index index.html index.jsp;
proxy_pass http://weixin.ddddd.com:8083;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name weixin.eeeee.com;
location / {
index index.html index.jsp;
proxy_pass http://weixin.eeeee.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name demo.wly.fffff.com;
location / {
index index.html index.jsp;
proxy_pass http://demo.wly.fffff.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 105.26.224.50;
location / {
index index.html index.jsp;
proxy_pass http://105.26.224.51;
proxy_set_header Host 105.26.224.50;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 105.26.224.52;
location / {
index index.html index.jsp;
proxy_pass http://105.26.224.53;
proxy_set_header Host 105.26.224.52; #這一行的作用是把原h(huán)ttp請(qǐng)求的Header中的Host字段也放到轉(zhuǎn)發(fā)的請(qǐng)求,如果不加nginx轉(zhuǎn)發(fā)的請(qǐng)求header里就不會(huì)有Host字段
proxy_set_header X-Real-IP $remote_addr; # 在web服務(wù)器端獲得用戶的真實(shí)ip
以下兩行是nginx反向代理配置websocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
以下三行是nginx設(shè)置超時(shí)配置
proxy_connect_timeout 75; 鏈接
proxy_read_timeout 600; 讀取
proxy_send_timeout 600; 發(fā)請(qǐng)求
以下一行配置是客戶端請(qǐng)求服務(wù)器最大允許大小
client_max_body_size 4098m
設(shè)置HTTP協(xié)議版本
proxy_http_version 1.1
proxy_cookie_domain參數(shù)的作用是轉(zhuǎn)換response的set-cookie header中的domain選項(xiàng),由后端設(shè)置的域名domain轉(zhuǎn)換成你的域名replacement,來保證cookie的順利傳遞并寫入到當(dāng)前頁(yè)面中,注意proxy_cookie_domain負(fù)責(zé)的只是處理response set-cookie頭中的domain屬性,如果set-cookie本身就沒有domain內(nèi)容則無需加
proxy_cookie_domain 10.23.1.1 10.23.1.2;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
配置https請(qǐng)求
server {
listen 443;
server_name localhost; #本機(jī)的IP地址
ssl on;
root html;
index index.html index.htm;
ssl_certificate cert/證書名稱.pem; 如:/etc/nginx/server.crt;
ssl_certificate_key cert/證書名稱.key; 如:/etc/nginx/server.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
#root html;
#index testssl.html index.html index.htm;
proxy_redirect off;
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://IP地址/ssl/;
}
}
}其他常用配置
配置1:
location /test/ {
proxy_pass http://10.23.2.96:8082/;
}- 訪問:http://127.0.0.1/test/ 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/
- 訪問:http://127.0.0.1/test/login 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/login
配置2:
location /test {
proxy_pass http://10.23.2.96:8082;
}- 訪問:http://127.0.0.1/test 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/test
- 訪問: http://127.0.0.1/test/ 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/test/
- 訪問:http://127.0.0.1/test/login 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/test/login
配置3:
location /test{
proxy_pass http://10.23.2.96:8082/define;
}- 訪問:http://127.0.0.1/test 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define
- 訪問:http://127.0.0.1/test/ 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define/
- 訪問:http://127.0.0.1/test/login 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define/login
配置4:
location /test/ {
proxy_pass http://10.23.2.96:8082;
}- 訪問:http://127.0.0.1/test 轉(zhuǎn)發(fā)到:http://192.168.2.96:8082/test/
- 訪問:http://127.0.0.1/test/ 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/test/
- 訪問:http://127.0.0.1/test/login 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/test/login
配置5:
location /test/ {
proxy_pass http://10.23.2.96:8082/define;
}- 訪問:http://127.0.0.1/test 轉(zhuǎn)發(fā)到:http://192.168.2.96:8082/test/
- 訪問:http://127.0.0.1/test/ 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define
- 訪問:http://127.0.0.1/test/login 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/definelogin #define與login之間不會(huì)有/ ,會(huì)直接拼接起來
配置6:
location /test{
proxy_pass http://10.23.2.96:8082/;
}- 訪問:http://127.0.0.1/test 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/
- 訪問:http://127.0.0.1/test/ 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082//
- 訪問:http://127.0.0.1/test/login 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082//login
配置7:
location /test {
proxy_pass http://10.23.2.96:8082/define/;
}- 訪問:http://127.0.0.1/test 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define/
- 訪問:http://127.0.0.1/test/ 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define//
- 訪問:http://127.0.0.1/test/login 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define//login
配置8:
location /test/ {
proxy_pass http://10.23.2.96:8082/define/;
}- 訪問:http://127.0.0.1/test 轉(zhuǎn)發(fā)到:http://127.0.0.1:8082/test/
- 訪問:http://127.0.0.1/test/ 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define/
- 訪問:http://127.0.0.1/test/login 轉(zhuǎn)發(fā)到:http://10.23.2.96:8082/define/login
備注:proxy_pass 后的URL如果符合 protocol://ip:port 同時(shí)結(jié)尾不加"/",則nginx會(huì)代理匹配路徑部分,否則不代理匹配路徑,同時(shí)自動(dòng)添加不匹配路徑"部分",比如/testone/login的/login部分
2、目錄白名單配置
配置1:校驗(yàn)一級(jí)目錄
nginx.conf配置如下:
server {
#配置校驗(yàn)一級(jí)路由白名單樣例
location /{
set $urlacl $uri;
if($urlacl ~ ^(/\w*).*)
{
set $urlacl /nginx/nginx/nginx/urlacl$1;
}
if(!-d $urlacl)
{
return 401;
}
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://21.13.245.185:8090; (轉(zhuǎn)發(fā)地址)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
expires -l;
}
}對(duì)應(yīng)目錄配置如下:

- 訪問:http://xxx.xxx.xx.xx:8443/reader/open 無法轉(zhuǎn)發(fā)到:http://21.13.245.185:8090/reader/open

- 訪問:http://xxx.xxx.xx.xx:8443/reader 無法轉(zhuǎn)發(fā)到:http://21.13.245.185:8090/reader
- 訪問:http://xxx.xxx.xx.xx:8443/minio/open 轉(zhuǎn)發(fā)到:http://21.13.245.185:8090/minio/open
- 訪問:http://xxx.xxx.xx.xx:8443/minio 轉(zhuǎn)發(fā)到:http://21.13.245.185:8090/minio
配置1:校驗(yàn)二級(jí)目錄
nginx.conf配置如下:
server {
#配置校驗(yàn)二級(jí)路由白名單樣例
location /minio{
set $urlacl $uri;
if($urlacl ~ ^(/minio\w*).*)
{
set $urlacl /nginx/nginx/nginx/urlacl$1;
}
if(!-d $urlacl)
{
return 401;
}
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://wly.test.com:8090/wps_result/open; (轉(zhuǎn)發(fā)地址)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
expires -l;
}
}對(duì)應(yīng)目錄配置如下:

- 訪問:http://127.0.0.1:8080/minio/test 無法轉(zhuǎn)發(fā)到:http://wly.test.com:8090/wps_result/open/minio/test
- 訪問:http://127.0.0.1:8080/minio/test/aa 無法轉(zhuǎn)發(fā)到:http://wly.test.com:8090/wps_result/open/minio/test/aa
- 訪問:http://127.0.0.1:8080/minio/adapter 轉(zhuǎn)發(fā)到:http://wly.test.com:8090/wps_result/open/minio/adapter
- 訪問:http://127.0.0.1:8080/minio/adapter/aa 轉(zhuǎn)發(fā)到:http://wly.test.com:8090/wps_result/open/minio/adapter/aa
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx反向代理中出現(xiàn)502錯(cuò)誤的解決步驟
反向代理是一種服務(wù)器代理的方式,它代理了客戶端的請(qǐng)求并將請(qǐng)求轉(zhuǎn)發(fā)給后端服務(wù)器,然后將后端服務(wù)器的響應(yīng)返回給客戶端,但經(jīng)常會(huì)遇到502錯(cuò)誤,所以本文給大家介紹了Nginx反向代理中出現(xiàn)502錯(cuò)誤的解決步驟,需要的朋友可以參考下2025-03-03
keepalived對(duì)nginx進(jìn)行高可用搭建及原理詳解
這篇文章主要為大家介紹了keepalived對(duì)nginx進(jìn)行高可用搭建及原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
詳解nginx 代理多個(gè)服務(wù)器(多個(gè)server方式)
本篇文章主要介紹了詳解nginx 代理多個(gè)服務(wù)器(多個(gè)server方式),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Nginx 請(qǐng)求壓縮的實(shí)現(xiàn)(動(dòng)態(tài)壓縮,靜態(tài)壓縮)
本文主要介紹了Nginx 請(qǐng)求壓縮的實(shí)現(xiàn)(動(dòng)態(tài)壓縮,靜態(tài)壓縮),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

