在 https://www.cnblogs.com/heyang78/p/15966992.html 里提到了如何部署靜態頁面網站,其中全用了用戶root,這有點不合理。
故此文采取訪問用戶ufo的網頁,只有修改nginx.conf和啟動nginx使用用戶root。
以ufo登錄linux后,在自己的目錄/home/ufo下創建www目錄,在其中創建一個名為index.html的文件,內容不用拘泥於html格式,像我就寫了一句Today is a nice day.
然后轉換成root用戶,進入目錄/usr/local/nginx/conf,修改 nginx.conf,
vi /usr/local/nginx/conf/nginx.conf
然后將全局用戶換成ufo,根目錄指定到/home/ufo/www
user ufo; 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; location / { root /home/ufo/www; index index.html index.htm; } #error_page 404 /404.html;
以上配置中,藍色為路徑,意圖標明修改點的位置,紅色部分為修改內容,其余部分不要亂動。
修改完畢后,保存退出。
執行 #/usr/local/nginx/sbin/nginx 啟動項目
或是執行 #/usr/local/nginx/sbin/nginx -s reload 重啟項目
然后訪問nginx所在機器的ip,就出現預期中的靜態畫面:
用靜態頁面替換默認畫面至此完成。
有些朋友嘗試時出現403錯誤一般為權限所致,查看/usr/local/nginx/logs/error.log可以看到出錯原因。
[root@localhost ~]# tail /usr/local/nginx/logs/error.log 2022/03/04 19:49:34 [error] 65230#0: *11 "/root/index.html" is forbidden (13: Permission denied), client: 192.168.245.1, server: localhost, request: "GET / HTTP/1.1", host: "192.168.245.128"
出現這種錯誤的根源是nginx開頭的user和創建根目錄和文件的用戶不一致造成的。
要想文件被正常訪問,那么創建文件是哪個用戶,nginx.conf開頭就該是這個用戶,即兩者都是上面提到的ufo。
再讓我們嘗試一個靜態圖片的展示
首先還是切換到ufo賬戶,進入/home/ufo/www下,
使用以下命令下載一張圖片:
[ufo@localhost www]$ wget https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.songshifu.com%2Falbum%2F490%2F342974%2Fc4143a7cada0ed93.jpg&refer=http%3A%2F%2Fpic.songshifu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649036500&t=56168c0ba34fa9dfda72e47bc9314604
然后改名:
[ufo@localhost www]$ mv src=http:%2F%2Fpic.songshifu.com%2Falbum%2F490%2F342974%2Fc4143a7cada0ed93.jpg 1.jpg
現在www目錄下有兩個文件了:
[ufo@localhost www]$ ls 1.jpg index.html [ufo@localhost www]$
讓我們輸入在瀏覽器 http://192.168.245.128/1.jpg 看圖片是否會正常出來:
確如預期,binggo!
再在/home/ufo/www下創建目錄test,然后下載一張圖片,更名為2.jpg,之后使用http://192.168.245.128/test/2.jpg瀏覽,結果也如預期:
看來server.location.root的設置讓客戶端擁有了瀏覽服務器指定目錄的權利。
END