使用nginx实现动静分离,访问ftp中的图片

在一个自己的写的小项目中,刚好使用了nginx。

做个笔记nginx的基本操作命令:

1、启动:

C:\server\nginx-1.0.2>start nginx

2、停止:

C:\server\nginx-1.0.2>nginx.exe -s stop

3、重新载入Nginx:

C:\server\nginx-1.0.2>nginx.exe -s reload

4、重新打开日志文件:

C:\server\nginx-1.0.2>nginx.exe -s reopen

5、查看Nginx版本:

C:\server\nginx-1.0.2>nginx -v

问题:

项目中有很多图片,所以我搭建的了一个ftp图片服务器来存放图片,

当我完成图片上传到ftp服务器中时,发现如果要在页面显示ftp上的图片,

通过百度查到

https://www.cnblogs.com/Donnnnnn/articles/10455084.html,

感觉方式都不怎么好,刚好nginx有一个动静分离的功能

nginx配置图片等静态文件路径:通过配置nginx的conf文件

   server {
        listen       8089;#这个是监听的端口
        server_name  localhost;

        #charset utf-8;

        #access_log  logs/host.access.log  main;
        
      #添加博客的代码
      location ~ .*\.(gif|jpg|jpeg|png)$ {  
      expires 24h;  
      root D:/PicTure/;#指定图片存放路径 ftp存放图片路径
      access_log D:/PicTure/log;#图片路径log自己在对应目录建一个  
      proxy_store on;  
      proxy_store_access user:rw group:rw all:rw;  
      proxy_temp_path         D:/PicTure/;#图片路径ftp存放图片路径
      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;#默认80端口代理访问地址(这个我暂时也不知道有啥用)
       }  
      }    

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

  

通过配置,我实现了用ftp服务器存放网站的图片,用nginx实现动静分离,可以通过ip加端口去访问该图片,

不用通过 ftp://FTP用户名:FTP密码@IP地址/FTP目录下图片名 这种方式,因为我感觉这样把用户名密码都暴露了很不安全。

还有通过读取图片流的方式,也很麻烦的感觉。

所以选择nginx配置来实现我需要的功能。

 

加油啊,小伙子哈哈哈哈哈哈

posted @ 2020-07-26 17:19  寻找现实的感觉  阅读(423)  评论(0编辑  收藏  举报