環(huán)境說明:
CentOS 6 64bit Mini最小化安裝
MongoDB 1.8.2 【安裝配置方法詳見:最簡單實用的MongoDB安裝教程:在CentOS中使用 yum 安裝MongoDB及服務(wù)器端配置詳解】
Nginx 1.0.5
本文將用費覆蓋方式安裝Nginx的擴展Nginx-Gridfs
Nginx的Nginx-Gridfs擴展地址:https://github.com/mdirolf/nginx-gridfs
可以使用wget下載,但是這個源碼中海鏈接著其它源碼,所有用git方法下載最方便,如果沒有安裝git的話按下面的步驟來
11 | Summary : Fast Version Control System |
14 | Description: Git is a fast, scalable, distributed revision control system with |
15 | : an unusually rich command set that provides both high-level |
16 | : operations and full access to internals. |
18 | : The git rpm installs the core tools with minimal dependencies. To |
19 | : install all git packages, including tools for integrating with |
20 | : other SCMs, install the git-all meta-package. |
Ok,git工具安裝完成,下一步下載nginx-gridfs源碼包
一、安裝nginx-gridfs擴展
05 | //進入我的nginx1.0.5的源碼目錄編譯安裝nginx-gridfs擴展 |
07 | //編譯前先查看現(xiàn)有的nginx的編譯參數(shù)配置 |
09 | nginx: TLS SNI support disabled |
10 | nginx: configure arguments: --user=www --group=www --prefix=/usr/ local /nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 |
11 | //編譯配置在原有配置基礎(chǔ)上增加新的擴展(藍色部分) |
Nginx的nginx-gridfs擴展模塊安裝完成,檢查一下吧
2 | nginx: nginx version: nginx/1.0.5 |
3 | nginx: built by gcc 4.4.4 20100726 (Red Hat 4.4.4-13) (GCC) |
4 | nginx: TLS SNI support enabled |
5 | nginx: configure arguments: --user=www --group=www --prefix=/usr/ local /nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=/root/nginx-gridfs |
二、在Nginx中配置nginx-gridfs
配置語法說明:
gridfs DB_NAME [root_collection=ROOT] [field=QUERY_FIELD] [type=QUERY_TYPE] [user=USERNAME] [pass=PASSWORD]
- gridfs 表示告訴nginx服務(wù)器要調(diào)用gridfs模塊
- root_collection= 指定Gridfs collection的前綴. 默認: fs
- field= 指定用于查詢的字段 可以是 _id 和 filename. 默認: _id
- type= 指定查詢的類型,這里支持 objectid, string 和int. 默認: objectid
- user= 指定數(shù)據(jù)庫的用戶名. 默認: NULL
- pass= 指定數(shù)據(jù)庫的密碼. 默認: NULL
Nginx配置文件中的具體寫法:
/usr/local/nginx/conf/nginx.conf
1 | 方法4,這種是一個完整參數(shù)的配置例子 location /static/ { |
*以上方法中 換行不是必要,僅僅是為了看的清晰
我的一個完整的配置
04 | server_name static.ebook.vm6; |
13 | log_format static.ebook.vm6 '$remote_addr - $remote_user [$time_local] $request ' |
14 | '$status $body_bytes_sent $http_referer ' |
15 | '$http_user_agent $http_x_forwarded_for' ; |
16 | access_log /home/wwwlogs/static.ebook.vm6.log static.ebook.vm6; |
注:在測試配置時要記住不要將nginx的文件過期緩存時間配置開啟了,最好是在配置好服務(wù)器后再做這個工作,否則很容易造成配置錯誤的假象。我在配置過程用就遇到了這樣的問題
到這里你也許可以正常使用Nginx-Gridfs了,其實不然。
當(dāng)你重新啟動系統(tǒng)后你會發(fā)現(xiàn)用瀏覽器訪問服務(wù)器上的web站點沒有響應(yīng),這是因為系統(tǒng)啟動過程中Nginx啟動比MongoDB早,它初始化的時候不能正確鏈接MongoDB數(shù)據(jù)庫。
了解關(guān)于CentOS的守護進程啟動順序相關(guān)的解釋可以看看百度百科的這篇文章:http://wenku.baidu.com/view/f13befcfa1c7aa00b52acb40.html
查看啟動順序:
2 | K10saslauthd K86cgred S02lvm2-monitor S11auditd S25netfs S55sshd S90crond |
3 | K50netconsole K87restorecond S08ip6tables S12rsyslog S26udev-post S64mysql S99local |
4 | K74ntpd K89rdisc S08iptables S22messagebus S50php-fpm S80postfix |
5 | K75ntpdate K95cgconfig S10network S24avahi-daemon S55nginx S85mongod |
2 | K10saslauthd K86cgred S02lvm2-monitor S11auditd S25netfs S55sshd S90crond |
3 | K50netconsole K87restorecond S08ip6tables S12rsyslog S26udev-post S64mysql S99local |
4 | K74ntpd K89rdisc S08iptables S22messagebus S50php-fpm S80postfix |
5 | K75ntpdate K95cgconfig S10network S24avahi-daemon S55nginx S85mongod |
這些都是鏈接到/etc/init.d/目錄里的相應(yīng)守護進程啟動腳本
S55nginx 意思是nginx守護進程啟動順序為55
S85mongod 意思是mongod守護進程的啟動順序是85,問題就在這mongod比nginx晚啟動
修改啟動順序,將mongod的啟動順序值改為比nginx小的數(shù)
[root@vm ~]# mv /etc/rc3.d/S85mongod /etc/rc3.d/S54mongod
[root@vm ~]# mv /etc/rc5.d/S85mongod /etc/rc5.d/S54mongod
ok,現(xiàn)在reboot重啟下看看是不是問題已經(jīng)不存在了!