Cainiao nginx Source Code Analysis configuration and deployment (1) Implement nginx "I love you" and nginx source code
Date: Nov 7th, 2014
I still remember that in the CSDN account leakage events of the past few years, statistics showed that the programmer's account contains the most love. Here I also use it as a standard, in this article, we will explain how to use Nginx to build a simple Web server that displays "I love you" on one machine. Let's try it together.
1. Nginx package
The latest nginx development version is 1.7.7:
- Nginx-1.7.7.tar.gz: Linux
- Windows: nginx-1.7.7.zip
Stable version 1.6.2:
- Nginx-1.6.2.tar.gz: Linux
- Windows: nginx-1.6.2.zip
You can use your operating system and the preferred selection program package. Here, nginx-1.7.7.tar.gz is used inCentOs 1, 6.4In the operating system.
2. Download, decompress, and install Nginx
Download and decompress, nginx-1.7.7.tar.gzCentOs 1, 6.4For example:
1: wget http://nginx.org/download/nginx-1.7.7.tar.gz
2: tar zxvf nginx-1.7.7.tar.gz
But do not rush to install Nginx after downloading and unzipping, because Nginx depends on a lot of software (gcc, g ++, make,libz-dev、
libbz2-dev、
libreadline-dev
), We assume that your Linux environment is "pure", so we will install these dependency packages below. Follow the steps below to install:
1: yum install gcc -y
2: yum install g++ -y
3: yum install make -y
4: yum install zlib-dev* -y
5: yum install bzip2-dev* -y
6: yum install readline-devel -y
This is some basic software, as well as the installation of PCRE. PCRE is short for "Perl Compatible Regular Expressions" and is a Regular expression library. Download, unzip, and install PCRE:
1: wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
2: tar -zxvf pcre-8.36.tar.gz
3: cd pcre-8.36.tar.gz
4: ./configure
5: make
6: make install
Now, we can install nginx.
1: tar -zxvf nginx-1.7.7.tar.gz
2: cd nginx-1.7.7
3: ./configure
4: sudo make
5: sudo make install
3. Nginx configuration file
Follow the above steps to install Nginx. Because we use the default configure/usr/local/nginx
Directory. The corresponding configuration file is/usr/local/nginx/conf/nginx.conf
. Open the configuration file and see the structure as follows:
1: ...
2: # working mode and maximum number of connections
3: events
4: {
5: # refer to the event model. use [kqueue | rtsig | epoll |/dev/poll | select | poll]; the epoll model is a high-performance network I/O model in the kernel of Linux 2.6 or later versions. If you are running on FreeBSD, use the kqueue model.
6: use epoll;
7: # maximum number of connections to a single process (maximum number of connections = number of connections * Number of processes)
8: worker_connections 65535;
9: }
10:
11: # Set the http server and use its reverse proxy function to provide Load Balancing support
12: http
13: {
14: ...
15: # First Virtual Server
16: server {
17: # Listen to port 80 of 192.168.8.x
18: listen 80;
19: server_name 192.168.8.x;
20: }
21: }
22:
Where
The events module indicates the working mode and the maximum number of connections. The http module sets the http server, and the server module indicates the virtual server. The simple configuration is listed here, for more information about configurations, see 《
Nginx configuration file nginx. conf
》。
4. Compile a simple Nginx Configuration
Create a/root/test_space directory to store our test page. Then, modify it directly in the default Nginx configuration file/usr/loca/nginx/conf/nginx. conf. Add a server module in http as follows:
1: server {
2: listen 8011;
3: server_name localhost;
4: charset utf-8;
5: location / {
6: alias /root/test_space/;
7: }
8: }
Wherelisten
Indicates the listening port number,sever_name
Isweb
Server Name (which can be a domain name, host, or IP address ),charset
Specifies the encoding character set.location
Then passalias
Specifiedweb
Service file directory.
5. Start Nginx
1: /usr/local/nginx/sbin/nginx
2: s -ef | grep nginx
If the following figure is displayed, the startup is successful:
6. Test
Create the following html file under/root/test_space:
1:
2:
3: <body>I love you!<body>
4:
-
Echo Chen: Blog.csdn.net/chen19870707
-
Apache and nginx are installed on the computer at the same time. After I enter an address in the browser, how can I determine whether it is resolved by apche or nginx?
These two cannot run at the same time. If both of them use 80, a prompt is displayed, indicating that the port is occupied! If you have to determine the software, just capture the package.
I just learned how to configure nginx + tomcat + JDK + MYSQL server with a free JSP source code. I don't know how to use it.
Download it from a5 or go to Sina sharing.
Hope to help you