nginx发布静态资源

nginx发布静态资源

参考

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

步骤

创建静态资源

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

静态资源结构

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端口根本没有服务提供者。

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的值以斜杠结尾报错。
    }
}

猜你喜欢

转载自www.cnblogs.com/mozq/p/11760874.html