Nginx修改静态文件访问路径

外网用户访问服务器的 Web 服务由 Nginx 提供,Nginx 需要配置静态资源的路径信息才能通过 url 正确访问到服务器上的静态资源。
打开 Nginx 的默认配置文件

vim /usr/local/nginx/conf/nginx.conf

将service中添加如下配置

root 静态文件根路径

示例代码:/etc/nginx/nginx.conf

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
    ‘$host "$request_uri" $status‘
    ‘"$http_referer" "$http_user_agent"‘;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
        client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
        server
        {
            listen 80 default;
            server_name _;
            root /var/ftp/source;
        }
        include /usr/local/nginx/conf/host/*.conf;
}

使用以下命令,重新加载Nginx

./nginx -s reload

再次通过IP和80端口访问时是以设置好的路径为根路径,可以依旧不同静态文件的路径访问静态文件

原文地址:https://blog.51cto.com/14233687/2473856

时间: 2024-08-15 04:44:12

Nginx修改静态文件访问路径的相关文章

nginx日志不记录静态文件访问和缓存

nginx访问日志nginx和apache的访问日志一样可以记录的指定信息,如记录服务器时间,访问的客户端ip.访问的url和访问状态码等信息,这些信息会规律的记录到访问日志中主配置文件中定义的日志格式,记录的格式参数解释如下 $remote_addr ? ? ? ? ? ? ? ? ? 客户端访问IP(公网IP) $http_x_forwarded_for ? ? ? ? ? 记录代理服务器的IP $time_local ? ? ? ? ? ? ? ? ? ? 日志中服务器本地时间 $host

nginx配置静态页面访问

server { server_name static.naice.me; // 你的域名或者 ip # 域名:static.naice.me 这里用 "_" 代表获取匹配所有 root /www/static-web/static-web; // 你的克隆到的项目路径 index index.html; // 显示首页 location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt){ root /www/static-web/static

Nginx 配置静态文件过期时间&防盗链

[[email protected] ~]# vim /usr/local/nginx/conf/vhosts/linux.conf location ~ .*\.(gif|jpeg|jpg|png|bmp|swf)$ { access_log off; expires 1d;  #过期时间 1天 } location ~ .*\.(js|css)  #此也也可改为  \.(js|css)来匹配 {     access_log off; expires 2h; } [[email protec

使用WSGIServer修改静态文件

背景:公司水务项目要求提供一个以POST方法修改静态文件的接口,由于nginx已经关闭了对POST方法的支持,并且开启POST方法后也无法去修改静态文件,所以需要额外的一个api接口去修改,而python的WSGIServer正好适用 一.使nginx支持POST方法 1.下载nginx的tar包:http://nginx.org/download/nginx-1.15.9.tar.gz 2.解压tar包后,修改src/http/modules/ngx_http_static_module.c文

NodeJS静态文件访问性能测试

受益于JavaScript语言优秀特点,使NodeJS开发服务端应用很方便,配合NPM官方提供的大量第三方组件,让NodeJS更加如虎添翼.最近,需要使用NodeJS做一个服务端HTML5的游戏服务器,应用实现包含两大部分:一部分做HTTP服务器为客户端发送html.image.javascript等游戏资源文件:另一部分使用了socket.io实时响应玩家的操作.技术实现并不困难,但要实现一个轻量.稳定.流畅的HTTP静态文件服务器,还是需要花些心思的. 本文笔者对现有一些HTTP服务器的实现

zbb20170726 spring访问静态文件访问

静态文件目录 web.xml拦截方式  / <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 默认/WEB-INF/[servlet名字]-servlet.xml加载上下文, 如果配置了

nodejs express 静态文件的路径

当express 设置为静态文件服务器的时候.可以通过2种方式进行方位: 1,通过设置app.use('路径1','../a/b/image') express 路径的形式,如 src="路径/1.jpg" 进行访问 2,通过原始的访问形式进行 如 src="../a/b/image" 注意点: 1,当有express使用了多个路径的时候如:app.use('路径1','../a/b/image').app.use('路径1','../aa/bb/image'):如

Eclipse中修改Web项目访问路径

背景 访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp  这里的xxxx,也就是request.getContextPath()得到的值. 我们在做系统开发的时候,在本地工作区可能会有同一个系统的多个版本存在,比如上面所说的xxxx系统,这里可能会有xxxx1.xxxx1_1.xxxx1_2代表该系统的1.0 .1.1. 1.2版本,对应的工程名字默认就是系统的访问路径,但是,我们可能会希望这些项目能够有一个统一

配置apache的文件访问路径

本例中,我们让apache访问"F:/testObject/php"路径: 一.修改http.conf文件配置 访问路径:"apache/conf/http.conf",修成这酱紫就行: DocumentRoot "F:/testObject/php" <Directory "F:/testObject/php"> 操作完成后,重启apache就可以了: 二.对于apache的某些版本,可能还需要下面的操作,修改h