在前兩篇博客中提到了搭建Nginx和Ftp服務器,在本篇博客,主要是介紹Nginx的配置文件的使用,怎樣修改配置文件使其成為一個圖片服務器。
一、Nginx圖片服務器配置
<span style="font-family:KaiTi_GB2312;font-size:18px;">[root@localhost sysconfig]# vi /etc/nginx/nginx.conf </span>
進入到了Nginx的配置文件頁面,然后將其修改為(請一定看代碼后面的備注說明):
<span style="font-family:KaiTi_GB2312;font-size:18px;">user nginx; worker_processes 1; #可修改,最大並發進程 error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; #可修改,最大並發量 } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; tcp_nodelay on; client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plainapplication/x-javascript text/css application/xml; gzip_vary on; #注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區 proxy_temp_path /usr/data0/proxy_temp_dir; #設置Web緩存區名稱為cache_one,內存緩存空間大小為200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小為5GB。 proxy_cache_path /usr/data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=5g; log_format cache '***$time_local ' '$upstream_cache_status ' 'Cache-Control: $upstream_http_cache_control ' 'Expires: $upstream_http_expires ' '"$request" ($status) ' '"$http_user_agent" '; #定義日志格式(此日志格式可以顯示hit miss等,顯示緩存是否被擊中,老版本默認可以,但是新版本,發現需要加上這個) access_log /var/log/nginx/cache.log cache; #使用這個日志格式 server #此處為緩存服務器 { listen 80; server_name 192.168.147.126; location / { proxy_cache cache_one; #對不同的HTTP狀態碼設置不同的緩存時間 proxy_cache_valid 200 304 12h; #以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內 proxy_cache_key $host$uri$is_args$args; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://192.168.147.126:8080; #此處跳轉到真實的圖片服務器 expires 1d; } #用於清除緩存,假設一個URL為http://192.168.8.42/test.txt,通過訪問http://192.168.8.42/public_root/test.txt就可以清除該URL的緩存。 location ~ /public_root(/.*) { #設置只允許指定的IP或IP段才可以清除URL緩存。 allow 127.0.0.1; deny all; #proxy_cache_purge cache_one$host$1$is_args$args; } #擴展名以.php、.jsp、.cgi結尾的動態應用程序不緩存。 location ~ .*\.(php|jsp|cgi)?$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log off; } <span style="color:#ff0000;">#真實的圖片服務器 server { listen 8080; server_name 192.168.147.126; location / { root /var; } access_log /var/log/nginx/access.log ; }</span> } </span>
備注:
1,192.168.147.126 為主機地址
2,紅色代碼部分,為主要部分(必要),其余部分,均可視自己的實際需求修改
3,本機的ftp上傳路徑為:/var / public_root/
4,listen監聽的端口,比如此處代碼的80和8080,需要在防火牆配置里面進行配置。命令行:
<span style="font-family:KaiTi_GB2312;font-size:18px;">[root@localhost sysconfig]# vim /etc/sysconfig/iptables</span>
配置完成后,記得重啟服務(iptables服務,Nginx服務),命令:service 服務名(nginx,iptables) restart
重要:更改完配置文件后,執行命令行:nginx -t,對配置文件進行測試,然后使用命令行:nginx -s reload,重新加載配置文件。
二、ftp文件上傳關鍵代碼
<span style="font-family:KaiTi_GB2312;font-size:18px;">@Test public void testFtpClient() throws Exception{ //1,創建一個FTPClient對象 FTPClient ftpCLient=new FTPClient(); //2,創建Ftp連接,默認是21端口 ftpCLient.connect("192.168.147.126",21); //3,登錄ftp服務器,使用用戶名和密碼 ftpCLient.login("HHX", "HHX"); //4,上傳文件 //4.1,讀取本地文件 FileInputStream inputStream=new FileInputStream(new File("K:\\Angel.jpg")); //4.2,設置上傳的路徑 ftpCLient.changeWorkingDirectory("/var/public_root"); //修改上傳文件的格式 //ftpCLient.setFileType( FTP.BINARY_FILE_TYPE); //4.3,第一個參數,服務器端文檔名;第二個參數,上傳的文檔inputstream ftpCLient.storeFile("Angel.jpg", inputStream); //5,關閉連接 ftpCLient.logout(); }</span>
三、訪問結果
四、總結
在這個過程中,還有可能會遇到因為網卡配置和DNS配置的一些問題,但一定要耐心和細心,不要着急。值得注意的是:ftp上傳文件路徑的授權;ftp登錄用戶的訪問文件權限。如果出現了403,那么可能會是兩種情況,第一檢查ip和端口是否和配置文件一致;第二,檢查訪問文件夾授權(區別777,755)。還有可能會出現404,這個需要檢查配置文件的server配置。
附:ftp配置說明(可能會出現用戶無法登陸的情況,這時候這些參數的配置很關鍵,vsftpd.conf)
vsftpd的配置,配置文件中限定了vsftpd用戶連接控制配置。
vsftpd.ftpusers:位於/etc目錄下。它指定了哪些用戶賬戶不能訪問FTP服務器,例如root等。
vsftpd.user_list:位於/etc目錄下。該文件里的用戶賬戶在默認情況下也不能訪問FTP服務器,僅當vsftpd .conf配置文件里啟用userlist_enable=NO選項時才允許訪問。
vsftpd.conf:位於/etc/vsftpd目錄下。來自定義用戶登錄控制、用戶權限控制、超時設置、服務器功能選項、服務器性能選項、服務器響應消息等FTP服務器的配置。
歡迎大家一起交流啊!