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

原文地址:https://www.cnblogs.com/mozq/p/11760874.html

时间: 2024-08-12 23:14:14

nginx发布静态资源的相关文章

linux使用Nginx搭建静态资源服务器

最近公司需要做一个宣传片播放  视频有点大 好几百M 就想到使用Nginx来代理静态资源,在过程中出现了一些问题,比如端口没开.访问是403等,没有成功,后面慢慢查找问题,才发现大部分博客资料的都不全,所以在这里记录一下. 安装过程本文就不提了 网上都有很多 本文主要说明 nginx.conf 的配置 如下: 进入编辑nginx.conf 文件 输入密码 配置nginx.conf 上传文件到配置的路径 在网页地址栏输入对应的地址 如果出现 nginx静态资源文件无法访问,403 forbidde

nginx缓存静态资源,只需几个配置提升10倍页面加载速度

nginx缓存静态资源,只需几个配置提升10倍页面加载速度 首先我们看图说话 这是在没有缓存的情况下,这个页面发送了很多静态资源的请求: 1.png 可以看到,静态资源占用了整个页面加载用时的90%以上,而且这个静态资源还是已经在我使用了nginx配置压缩以后的大小,如果没有对这些静态资源压缩的话,那么静态资源加载应该会占用这个页面展示99%以上的时间.听起来是不是已经被吓到了,但是数据已经摆在这里了,这可不是危言耸听. 然后再看看使用了nginx缓存之后的效果图: 2.png 看到没有,朋友们

百度UEditor图片上传、SpringMVC、Freemarker、Tomcat、Nginx、静态资源

个人官网.公司项目都需要 可视化编辑器,百度UEditor做得很不错,就用的这个.项目后台用到了SpringMVC.Freemarker,开发过程中部署在Jetty,线上部署用Tomcat,最后可能配置Nginx代理.     在实际使用过程中,遇到了太多的问题,因此有必要梳理和总结下. 1. 先说百度UEditor在Java环境中的使用:1.1   Html页面或者Freemarker模版里,引入百度UEditor的相关JS和CSS,如下 <script type="text/javas

利用Proxy Cache使Nginx对静态资源进行缓存

前言 Nginx是高性能的HTTP服务器,通过Proxy Cache可以使其对静态资源进行缓存.其原理就是把静态资源按照一定的规则存在本地硬盘,并且会在内存中缓存常用的资源,从而加快静态资源的响应. 配置Proxy Cache 以下为nginx配置片段: proxy_temp_path   /usr/local/nginx/proxy_temp_dir 1 2; #keys_zone=cache1:100m 表示这个zone名称为cache1,分配的内存大小为100MB #/usr/local/

HappyAA服务器部署笔记2(nginx的静态资源缓存配置)

我近期对服务器进行了少量改进,虽然之前使用了nginx反向代理之后性能有所提高,但仍然不够,需要使用缓存来大幅度提高静态资源的访问速度. 服务器上的静态资源主要有这些:png, jpg, svg, js, css等.下面,我通过新的nginx配置来实现缓存.对红色的字我会额外进行说明. worker_processes 1; events { worker_connections 1024; multi_accept on; use epoll; } http { include mime.ty

使用nginx处理静态资源请求,其余交给node

由于项目后台使用的是node,然而node不适合对静态资源的处理,因为他的异步处理(事件轮询)机制,所以更擅长的是密集I/O型的应用,所以我就有了一个想法,使用nginx来做反向代理,当请求的是静态资源的时候,直接由nginx(监听80端口)自己处理并返回,其他非静态资源请求转发至node(8080端口),由node来处理.下面是我的nginx配置文档,nginx安装请自行百度,大把资料啦~ #user nobody; worker_processes 1; #error_log logs/er

Nginx配置静态资源缓存时间及实现防盗链

环境源主机:192.168.10.158系统:centos 7.4域名:www.wuxier.cn盗链主机:192.168.10.191(使用Nginx+Tomcat实现负载均衡.动静分离的实验主机,点我进行复盘)系统:centos 7.4域名:www.ajie.com 和 www.taobao.com 创建软件包存放目录 [[email protected] ~]# mkdir /root/software [[email protected] ~]# cd /root/software/ [

nginx 代理静态资源报 403

用tomcat跑了一个上传服务,文件上传到指定nginx的html目录,用nginx来代理静态资源,结果上传能够成功,访问却报403. 解决办法,将html的拥有者改成nobody: chown -R nobody /usr/local/nginx/html 或者在nginx的配置文件nginx.conf中添加 use root; 之后刷新nginx的配置 /usr/local/nginx/bin/nginx -s reload 原文地址:https://www.cnblogs.com/flyi

nginx搭建静态资源服务器遇到403 Forbidden

1.在nginx中配置静态服务器 server { listen 91; server_name localhost; location / { root /root/software/qingfeng; index index.html; } } 这是我放静态资源的/root/software/qingfeng目录(注意:我这放在/root目录下) 我启动nginx时访问   ip地址:91时,访问不了,报403 Forbidden错误 2.我又从新在nginx中配置静态服务器 server