在Ubuntu上搭建圖片服務器(通過Nginx的反向代理)


引言:之前在自己電腦上裝完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對應端口也要打開)


開啟端口

完成之后就可以訪問了:


測試成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM