在Ubuntu上搭建简单图片服务器(通过Nginx反向代理)

2018-03-06  本文已影响0人  君名呼

引言:之前在自己电脑上装完Ubuntu之后,一直想要搭建图片服务器。今天花了3个小时时间,看着教程,自己慢慢摸索,总算全部弄完了。。。。

1、首先,你需要知道为什么要搭建图片服务器?

        以前看过很多的博主啊,大佬什么都说过,直接存图片在tomcat中不好,会出现各种问题。起初我也感觉不到什么问题。后来,在做到一个项目的时候,有许多的图片,一下子全部放到tomcat上(很多还是超清图片),站点直接爆炸了。访问起来太慢了,虽然最终在前辈的帮助下完成了,但是也坚定了我做图片服务器的心。

2、其次,你需要一些现成的材料。

pcre:Nginx在Ubuntu上需要依赖的一个库:

链接: https://pan.baidu.com/s/1mIH0xUdP-t3Y0QjulP2BWg 密码: z5at

zlib:Nginx在Ubuntu上需要依赖的另一个库:

链接: https://pan.baidu.com/s/1t9LRupRiRrBjVA3PO7o9TQ 密码: kdc4

Nginx:在Ubuntu上的包:

链接: https://pan.baidu.com/s/1U8lG9StMq0bIC6ldKCObNw 密码: wbpt

提示:这3个包最好放到一个文件夹中。

3、开始正式装载。

首先,将上面提到的三个包放到一个文件夹中。通过命令行先解压、安装上面两个包。

解压文件

类似于这种方式解压完之后,在Ubuntu上安装完两个依赖库(具体如何安装请百度)。

装完后对Nginx进行解压安装。(同上)

随后对这个文件进行修改:

对应的文件

修改内容如下:

#user nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

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;

    #gzip  on;

    server {

        listen      8088;

        server_name  192.168.237.128;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ~ .*\.(gif|jpg|jpeg|png)$ { 

            expires 24h; 

            root /home/david/images/;#指定图片存放路径 

            proxy_store on; 

            proxy_store_access user:rw group:rw all:rw; 

            proxy_temp_path        /home/david/images/;#代理临时路径

            proxy_redirect          off; 

            proxy_set_header        Host 192.168.237.128; 

            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://192.168.237.128:8088;#代理访问地址 

            } 

        }

        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;

        #}

    }

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen      8000;

    #    listen      somename:8080;

    #    server_name  somename  alias  another.alias;

    #    location / {

    #        root  html;

    #        index  index.html index.htm;

    #    }

    #}

    # HTTPS server

    #

    #server {

    #    listen      443 ssl;

    #    server_name  localhost;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;

    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers  on;

    #    location / {

    #        root  html;

    #        index  index.html index.htm;

    #    }

    #}

}

4、端口开启和测试图片

首先,在Ubuntu上启动Nginx的代理服务:

启动Nginx

启动之后访问在刚刚配置中指定文件夹的图片:

访问本地图片

随后到虚拟机外面开启外部端口:

我这边开启的是8088端口:(同时Ubuntu上的8088对应端口也要打开)

开启端口

完成之后就可以访问了:

上一篇 下一篇

猜你喜欢

热点阅读