nginx發佈靜態資源

nginx發佈靜態資源

參考

ngx_http_index_module index指令
ngx_http_core_module http指令 location指令 listen指令 root指令 server指令 server_name指令

Nginx之坑:徹底理解location中的index,配置網站初始頁
https://blog.csdn.net/qq_32331073/article/details/81945134

步驟

建立靜態資源html

conf/nginx.conf http模塊中新增server模塊nginx

靜態資源結構

E:\mozq\00store\frxx
├─frxx
│      bug.png
│      weixin.png

server模塊配置

server{
    listen 9001;
    server_name localhost;
    location / {
        root E:\mozq\00store\frxx; # root參數不能以斜槓結尾否則報錯。
    }
}
# 示例1 成功
http://localhost:9001/weixin.png
# 示例2 失敗
請求: http://localhost:9001
響應: 
    Request URL: http://localhost:9001/
    Request Method: GET
    Status Code: 403 Forbidden
# 示例3 失敗
請求: http://localhost:9001/weixin.pn
響應:
    Request URL: http://localhost:9001/weixin.pn
    Request Method: GET
    Status Code: 404 Not Found
# 示例4 失敗
請求: http://localhost:9002/
響應: 沒法訪問此網站。由於9002端口根本沒有服務提供者。
server {
    listen 9001;
    server_name localhost;

    location /fr {
        # root和alias都不能加斜槓,否則報錯。上面的/fr不能寫成/fr/。
        #root D:\00\frxx; # /fr/zhou.html 訪問 D:\00\frxx\fr\zhou.html
        alias D:\00\frxx; # /fr/zhou.html 訪問 D:\00\frxx\zhou.html
        #index chang.html;
    }
}

location塊

# location塊文檔
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
# nginx默認配置
location / {
    root   html; # 指定資源爲nginx主目錄下的html目錄。
    index  index.html index.htm; # 指定默認首頁列表
}

bugs

E:\mozq\chengxu\nginx\nginx-1.16.1>nginx -s reload
nginx: [emerg] unexpected "}" in E:\mozq\chengxu\nginx\nginx-1.16.1/conf/nginx.conf:40
錯誤代碼:沒有以分號結尾
location / {
    root E:\mozq\00store\frxx
}
正確代碼:以分號結尾
location / {
    root E:\mozq\00store\frxx;
}
E:\mozq\00store>nginx -s reload
nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)
2019/10/29 09:12:42 [notice] 9640#17752: using inherited sockets from "E:\mozq\chengxu\nginx\nginx-1.16.1"
2019/10/29 09:12:42 [emerg] 9640#17752: invalid socket number "E:\mozq\chengxu\nginx\nginx-1.16.1" in NGINX environment variable, ignoring the rest of the variable
2019/10/29 09:12:42 [emerg] 9640#17752: invalid socket number "E:\mozq\chengxu\nginx\nginx-1.16.1" in NGINX environment variable, ignoring
2019/10/29 09:12:42 [emerg] 9640#17752: CreateFile() "E:\mozq\00store/conf/nginx.conf" failed (3: The system cannot find the path specified)

E:\mozq\chengxu\nginx\nginx-1.16.1>nginx -s reload
nginx: [emerg] invalid socket number "E:\mozq\chengxu\nginx\nginx-1.16.1" in NGINX environment variable, ignoring the rest of the variable
nginx: [emerg] invalid socket number "E:\mozq\chengxu\nginx\nginx-1.16.1" in NGINX environment variable, ignoring
緣由:
    爲nginx設置環境變量爲NGINX和其源碼中衝突了。
    將環境變量名改成NGINX_HOME
C:\Users\1>nginx -s reload -c E:\mozq\chengxu\nginx\nginx-1.16.1\conf\nginx.conf
nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)
2019/10/29 13:44:10 [notice] 11572#8380: signal process started
2019/10/29 13:44:10 [error] 11572#8380: CreateFile() "C:\Users\1/logs/nginx.pid" failed (3: The system cannot find the path specified)
緣由:
    指定了配置文件,找不到其餘文件。使用-p參數指定nginx目錄
解決:使用 -p 參數指定 nginx 主目錄。
C:\Users\1>nginx -s reload -p E:\mozq\chengxu\nginx\nginx-1.16.1\
E:\mozq\chengxu\nginx\nginx-1.16.1>nginx -s reload
nginx: [emerg] unexpected "}" in E:\mozq\chengxu\nginx\nginx-1.16.1/conf/nginx.conf:40

錯誤代碼:root的值以斜槓結尾報錯。
server{
    listen 9001;
    server_name localhost;
    location / {
        root E:\mozq\00store\frxx\; # root的值以斜槓結尾報錯。
    }
}
相關文章
相關標籤/搜索