nginx发布静态资源

Posted 魔有追求

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx发布静态资源相关的知识,希望对你有一定的参考价值。

nginx发布静态资源

参考

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

步骤

创建静态资源

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

静态资源结构

E:mozq0storefrxx
├─frxx
│      bug.png
│      weixin.png

server模块配置

server{
    listen 9001;
    server_name localhost;
    location / {
        root E:mozq0storefrxx; # 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:mozqchengxu
ginx
ginx-1.16.1>nginx -s reload
nginx: [emerg] unexpected "}" in E:mozqchengxu
ginx
ginx-1.16.1/conf/nginx.conf:40
错误代码:没有以分号结尾
location / {
    root E:mozq0storefrxx
}
正确代码:以分号结尾
location / {
    root E:mozq0storefrxx;
}
E:mozq0store>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:mozqchengxu
ginx
ginx-1.16.1"
2019/10/29 09:12:42 [emerg] 9640#17752: invalid socket number "E:mozqchengxu
ginx
ginx-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:mozqchengxu
ginx
ginx-1.16.1" in NGINX environment variable, ignoring
2019/10/29 09:12:42 [emerg] 9640#17752: CreateFile() "E:mozq0store/conf/nginx.conf" failed (3: The system cannot find the path specified)

E:mozqchengxu
ginx
ginx-1.16.1>nginx -s reload
nginx: [emerg] invalid socket number "E:mozqchengxu
ginx
ginx-1.16.1" in NGINX environment variable, ignoring the rest of the variable
nginx: [emerg] invalid socket number "E:mozqchengxu
ginx
ginx-1.16.1" in NGINX environment variable, ignoring
原因:
    为nginx设置环境变量为NGINX和其源码中冲突了。
    将环境变量名改为NGINX_HOME
C:Users1>nginx -s reload -c E:mozqchengxu
ginx
ginx-1.16.1conf
ginx.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:Users1/logs/nginx.pid" failed (3: The system cannot find the path specified)
原因:
    指定了配置文件,找不到其他文件。使用-p参数指定nginx目录
解决:使用 -p 参数指定 nginx 主目录。
C:Users1>nginx -s reload -p E:mozqchengxu
ginx
ginx-1.16.1
E:mozqchengxu
ginx
ginx-1.16.1>nginx -s reload
nginx: [emerg] unexpected "}" in E:mozqchengxu
ginx
ginx-1.16.1/conf/nginx.conf:40

错误代码:root的值以斜杠结尾报错。
server{
    listen 9001;
    server_name localhost;
    location / {
        root E:mozq0storefrxx; # root的值以斜杠结尾报错。
    }
}

以上是关于nginx发布静态资源的主要内容,如果未能解决你的问题,请参考以下文章

nginx静态资源配置

Nginx静态资源服务器搭建

Nginx静态资源服务器搭建

nginx + tomcat 配置,静态资源直接使用nginx

Linux nginx

Nginx 动静分离概述