Linux使用Nginx搭建图片服务器


1 安装Nginx

Ubuntu下安装nginx及使用:https://blog.csdn.net/ZGL_cyy/article/details/117845308

安装包nginx的使用:https://blog.csdn.net/ZGL_cyy/article/details/105112050

docker部署nginx:https://blog.csdn.net/ZGL_cyy/article/details/111687007

Nginx配置文件详解:https://blog.csdn.net/ZGL_cyy/article/details/117845474

2 查看安装路径

#whereis nginx

3 配置nginx

     location ~ .*\.(gif|jpg|jpeg|png)$ {
            expires 24h;
            root /data/www/images/;#指定图片存放路径
            access_log /data/www/images/nginx/logs/images.log;#图片 日志路径
            proxy_store on;
            proxy_store_access user:rw group:rw all:rw;
            proxy_temp_path         /data/www/images/;#代理临时路径
            proxy_redirect          off;

            proxy_set_header        Host 127.0.0.1;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size    10m;
            client_body_buffer_size 1280k;
            proxy_connect_timeout   900;
            proxy_send_timeout      900;
            proxy_read_timeout      900;
            proxy_buffer_size       40k;
            proxy_buffers           40 320k;
            proxy_busy_buffers_size 640k;
            proxy_temp_file_write_size 640k;
            if ( !-e $request_filename)
            {
                 proxy_pass  http://127.0.0.1:8088;#代理访问地址
            }
        }

备注: /data/www/images/nginx/logs/、/data/www/images路径必须创建好。
在重启nginx服务之前,最好先测试一下nginx的配置文件。

4 测试配置文件

# nginx -t

备注:nginx是被我配置了全局软连接,所以可以不加绝对路径。

5 重启nginx服务

# nginx -s reload

6 测试访问图片

备注:将自己准备好的图片复制到/data/www/images下面,打开浏览器直接访问即可,注意端口配置为8088

7 重启nginx服务报错

1.nginx.conf 文件中配置的图片路径,如果没有事先创建好,就会报错。所以建议在重启服务之前先执行配置文件测试命令:nginx -t
2.nginx: [error] invalid PID number “” in “/usr/local/nginx/logs/nginx.pid”,这个错误是因为/usr/local/nginx/logs/nginx.pid不存在,或者内容为空导致的,那么我们只有执行如下命令即可:

# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

重启即可完成,然后访问。

8 访问图片服务器失败

设置路径权限,关闭防火墙,重新访问图片服务器。

#chmod 777 -R /data/www/images/
#ufw disable

备注:因为我的是linux mint系统所以我的开启关闭服务是通过ufw disable和ufw enable进行关闭防火墙和开启防火墙。

9 不重启nginx配置生效

输入命令:nginx -s reload

10 include配置使用例子

主模式配置

user  wwwt;	# 服务器使用用户
worker_processes  1;	# 配置 worker 进程启动的数量,建议配置为 CPU 核心数
#error_log  logs/error.log;	# 全局错误日志
pid    /etc/nginx/logs/nginx.pid; 	# 设置记录主进程 ID 的文件


events {
    # 单个后台 worker process 进程的最大并发链接数
    # 并发总数 max_clients = worker_professes * worker_connections
    worker_connections 4096;  ## Defaule: 1024
    # multi_accept on;  ## 指明 worker 进程立刻接受新的连接
}

# 主模式
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;
    # 重点,分文件放置路径
    include /etc/nginx/cs/*.conf;

    #gzip  on
    server {
        # the port your site will be served on
        listen 80;
        # the domain name it will serve for
        charset     utf-8;

        # max upload size
        client_max_body_size 75M;   # adjust to taste

        # Finally, send all non-media requests to the Django server.

        location / {

        }

    }

    }

分文件

server {
    # the port your site will be served on
    listen 443;
    # the domain name it will serve for
    server_name  cs.od888.cn; # substitute your machine's IP address or FQDN

    charset     utf-8;
    ssl   on;
    ssl_certificate      cert/*****.pem;
    ssl_certificate_key  cert/*****.key;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias ********;  # your Django project's media files - amend as required
    }
    location /static {
        alias ********; # your Django project's static files - amend as required
    }

    location / {
	    uwsgi_param UWSGI_SCHEME https;
        uwsgi_pass  127.0.0.1:9002;
        uwsgi_send_timeout 3600s;        # 指定向uWSGI传送请求的超时时间,完成握手后向	uWSGI传送请求的超时时间。
        uwsgi_connect_timeout 3600s;     # 指定连接到后端uWSGI的超时时间。
        uwsgi_read_timeout 3600s;        # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间。
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }

}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵广陆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值