- 安裝fastdfs-nginx-module
下載fastdfs-nginx-module
https://github.com/happyfish100/fastdfs-nginx-module/releases
解壓fastdfs-nginx-module_v1.16.tar.gz
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /home/sourcecode
- 重新編譯安裝nginx
運行如下命令查看nginx版本
nginx -v
運行如下命令查看之前安裝的nginx編譯參數
nginx -V
這是我本地虛擬機中nginx之前編譯的參數
--user=nginx --group=nginx --prefix=/usr/local/nginx --with-openssl=/home/sourcecode/openssl-1.1.1a --with-pcre=/home/sourcecode/pcre-8.42 --with-zlib=/home/sourcecode/zlib-1.2.11 --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-mail --with-mail_ssl_module --with-http_realip_module --with-http_stub_status_module --with-google_perftools_module --add-module=/home/sourcecode/ngx_cache_purge-2.3
進入到nginx源碼目錄(根據各自的nginx源碼存放路徑)
cd /home/sourcecode/nginx-1.14.2/
新建shell文件custom_configure.sh,
輸入vi custom_configure.sh,然後按i 進入編輯模式,將之前的編譯參數拷貝到文件中並加上fastdfs-nginx-module 模塊,如下:
vi custom_configure.sh
#!/bin/bash
./configure \
--user=nginx --group=nginx --prefix=/usr/local/nginx --with-openssl=/home/sourcecode/openssl-1.1.1a --with-pcre=/home/sourcecode/pcre-8.42 --with-zlib=/home/sourcecode/zlib-1.2.11 --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-mail --with-mail_ssl_module --with-http_realip_module --with-http_stub_status_module --with-google_perftools_module --add-module=/home/sourcecode/ngx_cache_purge-2.3 --add-module=/home/sourcecode/fastdfs-nginx-module/src
按ESC鍵然後輸入:wq保存custom_configure.sh文件
運行cat 命令查看是否有誤
cat custom_configure.sh
運行shell腳本
sh custom_configure.sh
備份nginx的配置文件
cp /usr/local/nginx/sbin/nginx /home/nginx/nginx.bak
cp /usr/local/nginx/conf/nginx.conf /home/nginx/nginx.conf.bak
cp /usr/local/nginx/conf.d/nginx.conf /home/nginx/conf.d/nginx.conf.bak
在/home/sourcecode/nginx-1.14.2/目錄執行安裝命令
make && make install
出現如下報錯:
/home/sourcecode/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c
In file included from /home/sourcecode/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:6:0:
/home/sourcecode/fastdfs-nginx-module/src/common.c:21:25: 致命錯誤:fdfs_define.h:沒有那個文件或目錄
修改fastdfs-nginx-module中的config文件
cd /home/sourcecode/fastdfs-nginx-module/src
vi config
將CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
改爲CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
修改前
修改後
重新執行shell腳本
sh custom_configure.sh
編譯安裝nginx
make && make install
- 修改nginx配置文件nginx.conf
cd /usr/local/nginx/conf
vi nginx.conf
在server模塊添加location /group1/M00
location / {
root /www/static/website;
index index.html index.htm;
}
location /group1/M00 {
ngx_fastdfs_module;
}
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|glb|ico)$ {
root /www/static/website;
expires 7d;
}
- 配置fastdfs-nginx-module 模塊
4.1 複製 mod_fastdfs_conf文件
cp /home/sourcecode/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
4.2 修改日誌存儲路徑
vi /etc/fdfs/mod_fastdfs.conf
修改日誌存儲路徑
base_path=/home/data/fastdfs/storage
4.3 修改tracker_server
tracker_server=192.168.98.22:22122
4.4 修改url_have_group_name爲true
url_have_group_name = true
4.5 修改圖片存儲路徑(必須要和storage.conf中的路徑一致)
store_path0=/home/data/fastdfs/storage
4.6 複製http.conf、mime.types配置文件
cp /home/appuser/software/fastdfs-5.05/conf/http.conf /etc/fdfs
cp /home/appuser/software/fastdfs-5.05/conf/mime.types /etc/fdfs
- 重新加載nginx配置文件
/usr/local/nginx/sbin/nginx -s reload
- 重啓fastdfs
service fdfs_trackerd restart
service fdfs_storaged restart
訪問fastdfs中的圖片
http://192.168.98.22/group1/M00/00/01/wKhiFlzKU_SAK70qAHItq7nb198241.jpg
遇到的問題
open() "/www/static/website/group1/M00/00/06/rBp0BV1qjh2AORYvAACsoDt2n2c775.png" failed (2: No such file or directory), client: 192.168.1.28, server: 192.168.98.22, request: "GET /group1/M00/00/06/rBp0BV1qjh2AORYvAACsoDt2n2c775.png HTTP/1.1"
,原因是匹配到了圖片的location
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|glb|ico)$ {
root /www/static/website;
expires 7d;
}
location優先級爲: = > ^~ > ~ 、~* > 常規字符串匹配
解決方法:將location修改爲
location ^~/group1/M00 {
ngx_fastdfs_module;
}
參考
https://www.hyxcable.com/a/1190000018251300