比如 我的域名為 explame.com, nginx 里網(wǎng)站目錄指向 public,nginx中默認(rèn)文檔已設(shè)為index.html
在public目錄下有個(gè) index.html
我想在直接訪問(wèn) http://explame.com 時(shí),能顯示靜態(tài)index.html里的內(nèi)容,而不必再經(jīng)由webman路由
upstream webman {
server 127.0.0.1:8788;
}
server
{
listen 80;
server_name ********;
index index.html;
root /www/wwwroot/ybtask/public;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
if (!-f $request_filename){
proxy_pass http://webman;
}
}
}
這樣配置后,直接訪問(wèn)域名是反代到了 webman,只有使用 http://explame.com/index.html 才能正確訪問(wèn)到。
同理如果public下有個(gè)ucenter目錄,而該目錄下有個(gè)index.html,也是只有使用 http://explame.com/ucenter/index.html 才能訪問(wèn)到這個(gè)index.html。如果省略了該index.html 就走到了webman。
嘗試過(guò) try_files $uri @webman;
這樣配置,也是一樣。
目前狀態(tài):
訪問(wèn) http://explame.com/ucenter/index.html ,沒(méi)有反代到webman,正常顯示 index.html里內(nèi)容,沒(méi)問(wèn)題
訪問(wèn) http://explame.com/api/getmyinfo ,正常反代到了 webman,也沒(méi)問(wèn)題
但如果 省略掉 index.html 去訪問(wèn) http://explame.com/ucenter 那么就被反代到了 webman,雖然本意只是想顯示 ucenter/index.html 這個(gè)文件
目的是想在訪問(wèn) http://explame.com/ucenter 這類(lèi)地址時(shí),先讓nginx判斷下是否存 http://explame.com/ucenter/index.html 文件,如果存在,那么就直接顯示這個(gè)文件,如果不存在,再反代到webman。
網(wǎng)站不應(yīng)該指向public目錄吧
應(yīng)該是只配置靜態(tài)文件走public就可以了。
不指向public,那應(yīng)該如何指向?
指向應(yīng)該沒(méi)問(wèn)題,現(xiàn)在的問(wèn)題是:訪問(wèn)某個(gè)目錄下的靜態(tài) index.html時(shí),無(wú)法省略index.html,雖然已在nginx里將默認(rèn)文檔設(shè)為了index.html
比如訪問(wèn) http://explame.com/ucenter/index.html ,一切正常,沒(méi)有反代到webman
比如訪問(wèn) http://explame.com/api/getmyinfo ,正常反代到 webman,也沒(méi)問(wèn)題
但如果 省略掉 index.html 去訪問(wèn) http://explame.com/ucenter 那么就被反代到了 webman,雖然本意是想顯示 ucenter/index.html 這個(gè)文件
其實(shí)就是說(shuō):在訪問(wèn) http://explame.com/api/getmyinfo 時(shí),想先讓nginx判斷下是否存 http://explame.com/api/getmyinfo/index.html 文件,如果存在,那么直接顯示這個(gè)文件,如果不存在,再反代到webman,就是這個(gè)效果
你說(shuō)的沒(méi)錯(cuò),但你沒(méi)明白我的意思,我是想訪問(wèn)public中某個(gè)目錄下的index.html文件時(shí),能實(shí)現(xiàn)省略index.html的目錄。
比如 public/abc/index.html 我是想 域名/abc 能實(shí)現(xiàn)和 域名/abc/index.html 一樣的效果
nginx 配置里加一句
index index.html;
比如
server {
listen 80;
server_name myadmin.com;
root /www/wwwroot/webman/public;
index index.html ;
.....
}
不是。
那個(gè)能設(shè)置,是要實(shí)現(xiàn)nginx的默認(rèn)文檔功能,比如nginx里默認(rèn)文檔 index.html。
訪問(wèn) /ucenter 時(shí),先判斷是否存在 /ucenter/index.html 這個(gè)文件,存在時(shí)就直接顯示,不存在的話再反代。
目前是訪問(wèn) /ucenter 時(shí),直接就反代到了 webman,雖然存在 /ucenter/index.html
我知道直接 /ucenter/index.html 是可以的,只是有需求,想在url中省略 這個(gè)index.html
location / {
error_page 405 = @webman;
try_files /${uri}/index.html /${uri} /${uri}/index.html @webman;
}
## WEBMAN LOCATION
location @webman {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8787;
}
剛測(cè)試了,沒(méi)問(wèn)題的。只要目錄里有index.html同時(shí)配置了index index.html,訪問(wèn)時(shí)加不加index.html都是先訪問(wèn)到index.html 。我是這么做的,假設(shè)靜態(tài)目錄的訪問(wèn)路徑為abc.com/public/,我就只配置了
location /public/ {
root /yourSitePath/;
}
按照這樣配置可以了。3q
=====
不想在訪問(wèn)時(shí)有public前綴。
其實(shí)這個(gè)就是想實(shí)現(xiàn)在 nginx+php-fpm時(shí)一樣的效果:public下是靜態(tài)文件,nginx里網(wǎng)站根目錄指向public(訪問(wèn)時(shí)url不帶有public)
public下有很多目錄和html文件,各個(gè)目錄下也有html文件,比如 index.html, ucenter,admin,doc,css,js等,在訪問(wèn)時(shí),如果指向的是這些目錄(如 /ucenter , /admin ),那么就直接顯示這些目錄下的 index.html。
在單純nginx+php-fpm時(shí),這些默認(rèn)都是可以的,只是加了反代配置后,如果url沒(méi)有精確匹配到 index.html 文件,就被反代了。
一直沒(méi)搞明白怎么配置。
現(xiàn)在明白了,多謝 各位的回答
如果要那樣在webman里應(yīng)該是不行的,你那種想法其實(shí)就是fpm所有框架的單入口,路徑逐個(gè)解析。但是在webman里默認(rèn)就限制只有public目錄可以被瀏覽器訪問(wèn),雖然public也可以改名,意義不大。如果你有多個(gè)前綴,那就多配置幾條規(guī)則就可以了。也不一定要是以public開(kāi)頭,只要root指向到webman里的public目錄就可以了。比如/ucenter/=>/public/ucenter,/admin=>/public/admin,如此如此