nginx靜態(tài)資源服務(wù)器怎么配置

進入nginx安裝目錄的conf目錄下,修改nginx.conf文件,在一個server{}中添加 一個location 部分配置代碼如下

root@ubuntu:/usr/local/nginx/conf#?vi?nginx.conf ?server?{ ????listen????80; ????server_name?localhost; ????location?/?{ ??????root??html; ??????index?index.html?index.htm; ????} ????location?/image/?{ ??????root??/usr/local/myimage/; ??????autoindex?on; ????}  ??}

從上面的配置可以看出來 端口為80,server_name為localhost(寫ip地址也可以)

location?/image/?{ ??????root??/usr/local/myimage/; ??????autoindex?on; ????}

這個配置表示輸入 localhost:80/image/ 時會訪問本機的/usr/local/myimage/image/ 目錄。所以要新建/usr/local/myimage/image/ 目錄,同時還要在nginx安裝目錄的html目錄中新建一個 與 location中 image同名的image目錄,雖然該目錄里面什么也沒有,在/usr/local/my image/image/ 中我們放一張圖片1.jpg上去,重啟nginx服務(wù),就可以通過 localhost:80/image/1.jpg訪問了

root@ubuntu:/usr/local/nginx/html#?mkdir?image  root@ubuntu:/usr/local/nginx/html#?mkdir?/usr/local/myimage/image #放一張照片上去# root@ubuntu:/usr/local/nginx/html#?cd?/usr/local/myimage/image root@ubuntu:/usr/local/myimage/image#?ls 1.jpg root@ubuntu:/usr/local/myimage/image#

重啟 nginx

root@ubuntu:/usr/local/nginx/sbin#?./nginx?-s?reload root@ubuntu:/usr/local/nginx/sbin#

打開瀏覽器 輸入 server_name:80/image/1.jpg 就可以訪問該靜態(tài)圖片了如下圖

nginx靜態(tài)資源服務(wù)器怎么配置

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊10 分享