Nginx反向代理靜態(tài)文件并修改路徑方式
更新時間:2025年03月19日 09:36:59 作者:草明
這篇文章主要介紹了Nginx反向代理靜態(tài)文件并修改路徑方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Nginx反向代理靜態(tài)文件并修改路徑
Nginx 配置想要將 /a/b/
的請求代理到本地目錄 /abc/
下的文件。可以在 Nginx 配置中使用 alias
指令來指定一個本地路徑作為代理目標(biāo)。
server { listen 8080; location / { proxy_pass http://192.168.1.100:8080; } location /a/b/ { alias /abc/; try_files $uri $uri/ /index.html; } }
解釋和注意事項:
location /a/b/
:這里配置了一個 location 塊,用于匹配以/a/b/
開頭的請求。alias /abc/;
:使用 alias 指令指定了本地路徑/abc/
作為代理目標(biāo)。當(dāng)匹配到/a/b/
的請求時,Nginx 將會將這些請求映射到本地目錄/abc/
。try_files $uri $uri/ /index.html;
:這里使用了try_files
指令,用于嘗試查找對應(yīng)的文件。如果請求的文件不存在,則會返回/index.html
。
注意:
- 在使用
alias
指令時,結(jié)尾的斜杠/
是重要的,確保路徑設(shè)置正確。 - 需要確保 Nginx 對
/a/b/
的訪問權(quán)限和路徑配置正確,以及本地目錄/abc/
中包含所需的靜態(tài)文件或資源。 - 配置完成后,重啟或重新加載 Nginx,然后嘗試訪問
/a/b/
下的資源,它應(yīng)該會被代理到本地目錄/abc/
中的對應(yīng)文件。
Nginx反向代理+路徑重寫 配置
#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 { 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; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # 訪問方式:http://127.0.0.1/1/ng1/ --> http://127.0.0.1:82 這里和方式二主要區(qū)別在于rewrite和proxy_pass的位置。 location ~ /ng1/ { proxy_pass http://127.0.0.1:81; rewrite "^/(.*)/ng1\/(.*)$" /$2 break; } # 訪問方式:http://127.0.0.1/1/ng2/ --> http://127.0.0.1:82 location ~ /ng2/ { rewrite "^/(.*)/ng2/(.*)$" /$2 break; proxy_pass http://127.0.0.1:82; } # 訪問方式:http://127.0.0.1/ng3/ --> http://127.0.0.1:83 location /ng3/ { rewrite ^/ng3/(.*)$ /$1 break; proxy_pass http://127.0.0.1:83; } #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; } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過Nginx反向代理實現(xiàn)IP訪問分流的示例代碼
本篇文章主要介紹了通過Nginx反向代理實現(xiàn)IP訪問分流的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Nginx服務(wù)器中配置非80端口的端口轉(zhuǎn)發(fā)方法詳解
這篇文章主要介紹了Nginx服務(wù)器中配置非80端口的端口轉(zhuǎn)發(fā)方法詳解,文中使用到了Nginx中的proxy_pass配置項,需要的朋友可以參考下2016-04-04