Use nginx-gridFS to read MongoDB images and files (why is it always unavailable ?)

Source: Internet
Author: User
Tags install mongodb mongodb server

Use nginx-gridFS to read MongoDB images and files (why is it always unavailable ?)

Recently deployed the company's server's nginx + mongodb + gridfs environment;

I searched for N documents, which are basically the same, and encountered many problems;

The following is a document:

Abstract: nginx-gridfs is an nginx extension module that allows you to directly access files on the GridFS File System of MongoDB and provide HTTP access.

1. Install mongodb

MongoDB does not need to be installed in linux. After downloading the installation package from the official website, decompress the package and run mongod to start the MongoDB server. Of course, mongod has many options to start, run mongod -- help to view all the selected items.

Note: Many people cannot access images and files after building them, which has a lot to do with the Mongodb configuration. Attach the mongodb I have successfully configured to directly decompress and use it. Click to download mongodb.

For example: (download the package above, decompress it, and run the following command to directly start the service, which is available for test)

./Mongod-port 10001 -- dbpath ../data/-- logpath ../log/mongodb. log -- fork

Stop MongoDB

Do not use kill-9 to close mongod! In this way, the database will ignore everything and directly kill the process, which will damage the data file.
The safe way is to use the kill-2 pid to disable mongod, that is, when the mongod process receives the close command, it will wait until the current operation or file allocation and other operations are completed, close all opened connections and refresh the cached data to the disk.
The most secure way is to end with the shutdown command.

> Use admin
Switched to db admin
> Db. shutdownServer ();

Ii. Install nginx and nginx-gridfs

Dependent libraries and tools

1

2

# Yum-y install pcre-devel openssl-devel zlib-devel

# Yum-y install gcc-c ++

The preceding dependent libraries can be manually installed if the automatic installation fails;

Pcre download https://sourceforge.net/projects/pcre/files/pcre/

Download nginx-gridfs source code

1

2

3

4

5

# Git clone https://github.com/mdirolf/nginx-gridfs.git

# Cd nginx-gridfs

# Git checkout v0.8

# Git submodule init

# Git submodule update

The above operation method can be locally uploaded to the server by GIT, or directly on the server by GIT

Download the nginx source code and compile and install it.

1

2

3

4

5

# Wget http://nginx.org/download/nginx-1.4.7.tar.gz

# Tar zxvf nginx-1.4.7.tar.gz

# Cd nginx-1.4.7

#./Configure -- prefix =/usr/local/nginx -- with-openssl =/usr/include/openssl -- add-module = ../nginx-gridfs/

# Make-j8 & make install-j8

Modify the/usr/local/nginx/conf/nginx. conf configuration file.

Note: nginx has two nginx. conf configuration files, so do not configure them correctly;

1

2

3

4

5

6

7

Location/girdfstest /{

Gridfs pics

Field = _ id

Type = objectid;

Mongo 127.0.0.1: 10001;

}


The configuration here is briefly described as follows:

Gridfstest: Access address

Pics: Database

Mongo 127.0.0.1: 10001 # mongo server address and port

--------------------------------------

Start the nginx Service

1

#/Usr/local/nginx/sbin/nginx

Reload/usr/local/nginx/sbin/nginx-s reload

3. Test


Upload a file

Root @ d1 :~ #./Program files put 1.jpg-db pics-t jpg
File in column warehouse picking:

Root @ d1 :~ # Program files list-db pics

Specific program files can be searched by Baidu

Or directly./program files -- help to view help

Browser access: http: // 192.168.4.156/girdfstest/1.jpg

If images can be accessed, the configuration is successful;

Note:

Various Errors may occur during the installation process, depending on the environment;

Important Notes

1. Nginx startup parameters, which must be associated with the nginx-gridfs directory

2. Try not to use the default port 27017 in Mongodb and use another port (the default port cannot be accessed during the installation process. I do not know why)

3. Configure Nginx. conf. The parameters are described in the preceding document.

4. If the configuration fails, the image cannot be accessed. Check the nginx error log for more information.

If you have other problems during the installation process, you can refer to the documents I wrote earlier;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.