nginx搭建文件服务器(指定目录)

通过yum install -y nginx安装,配置文件是/etc/nginx/nginx.conf。

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       8055 default_server;
        listen       [::]:8055 default_server;
        server_name  _;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
            root    /usr/share/nginx/html/download;
            autoindex on;    #开启索引功能
            autoindex_exact_size off;  #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
            autoindex_localtime on;   #显示本机时间而非 GMT 时间
	    #index index.html;
        }


        location /nginxdata {
            #alias /nginxdata/;
            root /;
            autoindex on; # 开启目录文件列表
            autoindex_exact_size on; # 显示出文件的确切大小,单位是bytes
            autoindex_localtime on; # 显示的文件时间为文件的服务器时间
            charset utf-8,gbk; # 避免中文乱码
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

其中第二个location指定的是该图的路径:
指定路径的

nginx命令:

启动:nginx
关闭:

  • nginx -s quit 此方式停止步骤是待nginx进程处理任务完毕进行停止
  • nginx -s stop 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程
    重新加载配置:nginx -s reload

可能遇到的错误排查:

1.最重要的就是查看自己的路径端口是不是有问题。

2.检查nginx使用的Linux的端口是否未开启,一般错误样子为下图:
错误例子
解决:打开端口:/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

3.403问题,查看Linux的selinux服务是否关闭:
403问题
解决办法:getenforce查看状态:Enforcing为开启,Permissive为关闭。
使用 setenforce 0 命令关闭selinux服务,再次访问成功。

问题:
如果需要配置多个静态资源,需要设置一个root,其他设置为alias,例如:
...略略略

    server {
        listen       8089 default_server;
        server_name  xxx.com default_server;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            root /jiao/instance_code/desc_time/;
        }

        location /resume { # 静态资源路径 ,全路径为html/static/resume
	    	alias  /jiao/resume/; # index路径
        }

        error_page 404 /404.html;
        location = /404.html {
		    root html;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }

        location ~ ^/favicon\.ico$ {
            root html;
        }

        location  ~ .*\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt|woff|ttf)$ {
	    	root html/static; # 这里设置了所有的文件静态路径
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值