windows下配置nginx访问本地静态资源,完整配置,使用root和alias的区别,小心掉坑里

需求

我有几个html,css,js文件,需要本地启动nginx访问,以前觉得很简单,但是踩了一些坑之后,做个记录

nginx完整配置


#user  nobody;
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;
	upstream manage{
		server localhost:8765; 
	}
	
    server {
        listen       8765;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root html;
            index  debug-all.html index.html;
        }
		
		location /vision-push {
            root D:\jar;
            index  debug-all.html index.html;
        }
		
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

访问

http://localhost:8765/vision-push/

使用root的配置的说明

原始文件在D:\jar\vision-push2/debug-all.html下

访问地址:http://localhost:8765/vision-push2

nginx拼接地址:D:\jar\vision-push,会把vision-push带上

location /vision-push {
            root D:\jar;
            index  debug-all.html index.html;
        }

使用alias配置说明

原始文件在D:\jar\vision-push2/debug-all.html下

访问地址:http://localhost:8765/vision-push

nginx拼接地址:D:\jar\vision-push,会把vision-push带上

        location /vision-push {
            alias D:\jar\vision-push;
            index  debug-all.html index.html;
        }

root和alias完整对比

		location /vision-push {
            root D:\jar;
            index  debug-all.html index.html;
        }
		
		location /vision-push {
            alias D:\jar\vision-push;
            index  debug-all.html index.html;
        }

 

 

 

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Nginx配置访问图片路径和静态页面非常简单。以下是一个示例配置文件的示例: ``` server { listen 80; server_name yourdomain.com; root /path/to/your/website; location /images { alias /path/to/your/images; autoindex on; } location /static { alias /path/to/your/static/files; autoindex on; } location / { try_files $uri $uri/ /index.html; } } ``` 在上述配置文件中,我们定义了一个虚拟主机,监听80端口,并使用`yourdomain.com`作为服务器名。 - 配置访问图片路径:使用`location /images`指令来配置访问图片的路径。将`/path/to/your/images`替换为实际存储图片的目录路径。如果需要显示图片列表,可以使用`autoindex on`指令。 - 配置访问静态页面:使用`location /static`指令来配置访问静态页面的路径。将`/path/to/your/static/files`替换为实际存储静态页面的目录路径。同样,如果需要显示文件列表,可以使用`autoindex on`指令。 - 其他路径:对于其他路径,我们使用`location /`指令,并使用`try_files`指令来尝试查找匹配的URI,如果找不到则返回`index.html`。你可以将`index.html`替换为你的默认页面。 请确保在配置文件中替换实际的路径和域名,然后使用`nginx -s reload`命令重新加载配置文件,以使配置生效。 以上配置允许通过`yourdomain.com/images`访问图片文件和列表,通过`yourdomain.com/static`访问静态页面和文件,还可以通过`yourdomain.com`访问默认页面或其他匹配的页面。 希望以上信息能够帮助到你!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值