ubuntu:18.04+nginx 搭建图片文件服务器

2019-01-04  本文已影响0人  T_zw

安装:

sudo apt-get install nginx

启动:

nginx -c /etc/nginx/nginx.conf

或:

/etc/init.d/nginx start

可以创建/etc/nginx/server文件夹,将server文件放在此文件夹中:

server文件(此文件中的/home/user/www是自定义的路径,并将需要访问的资源文件放在此文件夹中):
server {
        listen       8080;
        server_name  localhost;


        location ~ \.(jpg|png|jpeg|gif|bmp)$ {
                root /home/user/www;    #需要手动去创建的文件夹
        }
#       location /{
#
#               root /home/user/www;
#       }

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

}

进入到/etc/nginx/nginx.conf文件,将刚创建的server文件包含进来:

include /etc/nginx/server/*;

此行配置需要放在http{}中:

http{
  include /etc/nginx/server/*
}
http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
# gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##
        include /etc/nginx/server/*;
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

然后检查nginx.conf是否有问题:

nginx -t -c /etc/nginx/nginx.conf

若没问题,会出现如下提示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx
/etc/init.d/nginx restart

然后访问 ip:8080+图片名称

上一篇下一篇

猜你喜欢

热点阅读