nginx+tomcat叢集上傳圖片
1.思路
1. 一般使用fastdfs分散式檔案系統來處理(不支援window),因為部署的環境是window,所以這裡對fastdfs不過多的闡述。
2.實現叢集環境圖片上傳,我是通過nginx+ftp實現的。ftp伺服器上傳圖片到固定位置。nginx負責回顯資料。----下面採用的是這種方案。
3. 兩張表,第一個表是伺服器配置表(記錄伺服器的ip和埠) ,第二個表示記錄伺服器下對應的圖片資訊。假設有3個伺服器。
使用這種方案,理論情況下,圖片會分割成3份,每臺伺服器的圖片一樣多,實際上不可能一樣多,這裡就需要在資料庫做一個標識,後臺再查下資料庫的圖片數量,通過一定的演算法,從而保證圖片幾乎一樣多。
2.ftp程式碼
https://blog.csdn.net/qq_16855077/article/details/83862161
3.nginx
3.1nginx下載
官網地址:https://nginx.org/en/download.html
因部署系統為window,所以我下載的是包含windows的zip
把下載好的zip解壓
我看網上很多的人都是直接雙擊nginx,但是出現的相關都是一閃而過,用過tomcat的朋友,都知道一閃而過實際上報錯,我最初也是以為報錯,但是實際執行時沒有問題的。
建議通過cmd命令列的方式訪問
開始命令:start nginx
停止命令:nginx -s stop
重新載入配置 nginx -s reload 一般改動nginx配置後不用充錢nginx,重新載入配置就可以
網站訪問地址:http://localhost
nginx安裝成功效果圖如下:
3.2 通過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 8089; server_name localhost; #charset utf-8; #access_log logs/host.access.log main; #新增部落格的程式碼 location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; root D:\apache-tomcat-8.5.32/;#指定圖片存放路徑 access_log D:\apache-tomcat-8.5.32;#圖片路徑 proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path D:\apache-tomcat-8.5.32/;#圖片路徑 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://192.168.1.155:8089; #代理訪問地址 } } 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; # } #} }
window版本:
上面這兩個地址發到linux需要遮蔽掉,不然會修改圖片的所屬組
linux版本:
存放路徑
重新載入nginx配置,訪問http://localhost:8089/1.jpg
這樣就可以實現tomcat叢集圖片上傳勒,不管請求通過是有A、B、C伺服器發過來的,假設ftp伺服器在A,我們只需要把圖片儲存到A伺服器上就行,在A伺服器上搭建nginx和ftp伺服器,如果擔心A伺服器掛掉,導致圖片伺服器不可用的朋友,可以研究下ftp伺服器如何搭建。