nginx代理文件目錄、下載站點方式
前言
Nginx默認是不允許列出整個目錄瀏覽下載。
如果只是單純的往html文件中添加壓縮文件,網(wǎng)頁就會報錯,那該怎么才能達到一堆壓縮文件都顯示在網(wǎng)頁呢
一、訪問站點配置
先上配置再解釋
location /mylog {
autoindex on;
charset utf-8;
autoindex_exact_size off;
autoindex_localtime off;
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file /usr/local/mdtassistant/nginx/users;
alias /usr/local/mdtassistant/version;
}- 效果

- 帶密碼效果

解釋如下
- autoindex on; 模塊顯示文件
- charset utf-8; 文件編碼
- autoindex_exact_size off | on 默認為on, 顯示出?件的確切??,單位是bytes; 修改為off,顯示出?件的?概??,單位是kB或者MB或者GB
- autoindex_localtime on | off 默認為off,顯示的?件時間為GMT時間;修改為on, 顯示的?件時間為?件的服務(wù)器時間(這個是文件上傳的時間)
那 這兩個干嘛的
auth_basic “Auth access Blog Input your Passwd!”; auth_basic_user_file /usr/local/mdtassistant/nginx/users;
訪問時輸入密碼的
二、添加登錄權(quán)限功能
1.密碼生成
使用htpasswd工具生成密碼。
如果沒有htpasswd工具,可以先進行安裝,安裝命令:
yum -y install htpasswd
如果這個安裝不了就裝下面這個
yum install httpd-tools
密碼生成命令格式:htpasswd -c 存放用戶名密碼的文件路徑 用戶名
htpasswd -c /usr/local/nginx/passwd/users lc

提示輸入密碼,輸入兩次

然后會生成一個加密串,這樣就好了

2.配置nignx
auth_basic "Auth access Blog Input your Passwd!"; auth_basic_user_file /usr/local/mdtassistant/nginx/users;
auth_basic_user_file 密碼文件存放位置
三、路徑加 / 如何區(qū)分
- 如果proxy_pass末尾有斜杠/,proxy_pass不拼接location的路徑
- 如果proxy_pass末尾無斜杠/,proxy_pass會拼接location的路徑
1.proxy_pass末尾有斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000/;
}
- 請求地址:http://localhost/api/test
- 轉(zhuǎn)發(fā)地址:http://127.0.0.1:8000/test
2.proxy_pass末尾無斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000;
}- 請求地址:http://localhost/api/test
- 轉(zhuǎn)發(fā)地址:http://127.0.0.1:8000/api/test
3.proxy_pass包含路徑,且末尾有斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000/user/;
}- 請求地址:http://localhost/api/test
- 轉(zhuǎn)發(fā)地址:http://127.0.0.1:8000/user/test
4.proxy_pass包含路徑,末尾無斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000/user;
}- 請求地址:http://localhost/api/test
- 轉(zhuǎn)發(fā)地址:http://127.0.0.1:8000/usertest
四、文件路徑 alias與root區(qū)別
- root的處理結(jié)果是:root路徑+location路徑
- alias的處理結(jié)果是:使用alias路徑替換location路徑
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Nginx實現(xiàn)端口轉(zhuǎn)發(fā)TCP代理的實現(xiàn)示例
本文主要介紹了使用Nginx實現(xiàn)端口轉(zhuǎn)發(fā)TCP代理的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
nginx通過nginx_upstream_check_module實現(xiàn)后端健康檢查
nginx的健康檢查有兩種,一種是被動健康檢查,也就是nginx自帶健康檢查模塊ngx_http_upstream_module,另一種就是主動健康檢查,使用第三方模塊nginx_upstream_check_module,下面就來介紹一下,感興趣的可以了解一下2024-08-08
Nginx $remote_addr和$proxy_add_x_forwarded_for變量的實現(xiàn)
本文主要介紹了Nginx $remote_addr和$proxy_add_x_forwarded_for變量的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
基于nginx設(shè)置瀏覽器協(xié)商緩存過程詳解
這篇文章主要介紹了基于nginx設(shè)置瀏覽器協(xié)商緩存過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12

