: This article mainly introduces nginx Study Notes 1 using nginx to build a simple HTTP server. if you are interested in PHP tutorials, refer to it. As a lightweight http server, nginx can handle highly concurrent http requests. It can also be configured as an http proxy server. As a rookie in the background development, such an excellent open-source server is at the forefront, and it is always necessary to study and learn something to be reliable!
Of course, the first step to learning nginx is to learn how to use it.
1. install nginx
First, download the source code http://nginx.org/download/nginx-1.8.0.tar.gz from the official website.
Nginx depends on the pcre library, so you also need to download the pcre source code.
Decompress the pcre source code in the nginx-1.8.0/3 rdparty/pcre-8.32, and then execute
./configure --with-http_ssl_module --with-pcre=./3rdparty/pcre-8.32/./make./sudo make install
The installation is completed. the default installation directory is/usr/local/nginx.
During installation, you may be prompted that the openssl library is missing. you can directly use apt-get install.
2 Configure nginx
Before using nginx, create an nginx directory under/home/bookxiao/to store resource files, and then create a soft connection to conf, sbin, and logs under/usr/local/nginx, avoid frequent directory switching.
bookxiao@ubuntuforfun:~/nginx$ lsbin conf data logs
The nginx configuration file is located under conf/and is named nginx. conf. The basic unit that makes up the configuration file is a "directive", which consists of the name, parameter attribute, and the ':' At the end. In addition, some commands are surrounded by {} to form a block. if the block contains some instructions, the block is also called a "context )".
All commands in the configuration file are either in a certain context or are not contained in any context (that is, in the main context), which is somewhat similar to the global variables in C.
For example, a simple configuration file is as follows:
bookxiao@ubuntuforfun:~/nginx/conf$ cat nginx.conf#user nobody;worker_processes 1;events { worker_connections 1024;}http { server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /images { root /home/bookxiao/nginx/data; } }}
Event, http, and server are all "context"; command "worker_processes 1;" is located in main context.
To test nginx functions, first check how to configure server {} blocks. In nginx, each server block represents an HTTP service. different servers have different ports and server_name.
Assume that we have an image. the storage path is ~ /Nginx/data/images/beautiful-girl.jpg.
Now we want to enterhttp://127.0.0.1/images/beautiful-girl.jpg
, It will be like this:
Take a closer look at the configuration file above and see that there is a location block defined as follows:
location /images { root /home/bookxiao/nginx/data; }
The "/images" followed by locations is used to match the prefix of the client's URL request. If yes, the root value is added to the front end of the URL, form a new URL to go to the file system to find the resource file.
For example, the original URL here is "/images/beautiful-girl.jpg", then the final URL is "/home/bookxiao/nginx/data/images/beautiful-girl.jpg ".
What if multiple locations match a URL? The nginx policy selects the location with the longest matching value.
Therefore, we can add such a server:
server { listen 9999; server_name localhost_test; location /images { root /home/bookxiao/nginx/data/test; } }
Then executesudo nginx -s reload
Reload the configuration file. Create a test/images/directory under data. In this way, we enterhttp://127.0.0.1:9999/images/beautiful-girl.img
. Then we will see:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces nginx Study Notes 1 using nginx to build a simple HTTP server, including some content, hope to be helpful to friends who are interested in PHP tutorials.