Install nginx and use it under linux (super detailed, screenshots at every step)

1. Environmental installation

1.1 Environment installation of gcc

yum install gcc-c++
Check the version after installation
gcc -v
Insert picture description here

1.2 Third-party development kit installation

The author uses yum installation.
(If using compressed tar.gz package to install, use the following command
directory after decompression cd
./configure
the make
the make the install)

1.2.1 OpenSSL

OpenSSL is a powerful secure socket layer cryptographic library, including the main cryptographic algorithms, commonly used key and certificate packaging management functions and SSL protocols, and provides a wealth of applications for testing or other purposes. Nginx not only supports the http protocol, but also supports https (that is, HTTP is transmitted over the ssl protocol), so you need to install the openssl library in Linux.
yum install -y openssl openssl-devel
Insert picture description here

The installation is complete
Insert picture description here

1.2.2 zlib

The zlib library provides many ways to compress and decompress. nginx uses zlib to gzip the contents of the http package, so you need to install the zlib library on Linux.
yum install -y zlib zlib-devel
Insert picture description here
You can cat /usr/lib64/pkgconfig/zlib.pccsee the version number
Insert picture description here

1.2.3 PCRE

PCRE (Perl Compatible Regular Expressions) is a Perl library, including Perl compatible regular expression library. The http module of nginx uses pcre to parse regular expressions, so the pcre library needs to be installed on linux.
yum install -y pcre pcre-devel
Note: pcre-devel is a secondary development library developed by pcre. Nginx also requires this library.
Insert picture description here
The author has installed it, you can pcre-config --versioncheck the version number
Insert picture description here

2. nginx installation and startup

2.1 Nginx download

Download nginx from the official website: http://nginx.org/
download tar.gz ending

2.2 Nginx installation

Enter the /usr/localdirectory
This directory is the
cd /usr/local
Insert picture description here
first step to store some things installed by the user : upload the source package nginx-1.8.0.tar.gz of nginx to the linux system (I installed lrzsz, and then enter rz, and the upload will pop up File directory, user selection) sz is to download. Otherwise you have your own Baidu
Insert picture description here
Step two: Unpack the
tar zxvf nginx-1.8.0.tar.gz
Insert picture description here
Insert picture description here
third step: enter nginx-1.8.0 directory
Insert picture description here
can see there is a configure executable file, green for executable files
created makeFile file using the configure command.

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

You can see the Makefile after execution
Insert picture description here

Makefile is a configuration file. The source files in a project of Makefile are not counted. They are placed in several directories according to type, function, and module. Makefile defines a series of rules to specify which files need to be compiled first, and which files Need post-compilation, which files need to be recompiled, or even more complex functional operations, because the makefile is like a Shell script, which can also execute operating system commands.
configure parameters

./configure \
--prefix=/usr \                                                        指向安装目录
--sbin-path=/usr/sbin/nginx \                                 指向(执行)程序文件(nginx)
--conf-path=/etc/nginx/nginx.conf \                      指向配置文件
--error-log-path=/var/log/nginx/error.log \              指向log
--http-log-path=/var/log/nginx/access.log \            指向http-log
--pid-path=/var/run/nginx/nginx.pid \                      指向pid
--lock-path=/var/lock/nginx.lock \                         (安装文件锁定,防止安装文件被别人利用,或自己误操作。)
--user=nginx \
--group=nginx \
--with-http_ssl_module \                      启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)
--with-http_flv_module \                       启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_stub_status_module \     启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
--with-http_gzip_static_module \   启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
--http-client-body-temp-path=/var/tmp/nginx/client/ \ 设定http客户端请求临时文件路径
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 设定http代理临时文件路径
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 设定http fastcgi临时文件路径
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 设定http uwsgi临时文件路径
--http-scgi-temp-path=/var/tmp/nginx/scgi \ 设定http scgi临时文件路径
--with-pcre 启用pcre库

Step 4: Compile

make
After typing make, you can see that a lot of c files are generated
Insert picture description here

Step 5: Installation
make install
Insert picture description here

Note: Before starting nginx, because the configure command above specifies the temporary file directory as a /var/temp/nginx/client
reference--http-client-body-temp-path=/var/temp/nginx/client \

So you need /var/temp/nginx/clientto create this directory under
mkdir /var/temp/nginx/client -p
Insert picture description here

Go back to the view /usr/localdirectory, you can see that there is one more nginx directory
Insert picture description here

Enter the nginx directory and you can see that there are three folders
Insert picture description here

conf is the directory where the configuration file is located, html is the default directory where the static files are located (you can modify the location of the static files, don’t put them here), and sbin is the directory where the binary commands are located

Enter the sbin directory under the Nginx directory,
cd sbin/
you can see that there is only one nginx
Insert picture description here

2.3 nginx start

Enter the command to start Nginx
./nginx
Insert picture description here

Then go to the browser to view the address bar and enter the virtual machine ip. The default port is 80 so you don't need to bring it.
Insert picture description here

2.4 nginx exit and reload

./nginx -s stopAbnormal exit, which is equivalent to killing the process
./nginx -s quitnormally exiting
./nginx -s reloadand reloading the configuration file, that is, loading the Makefile above

3. Nginx static website deployment

3.1 Deployment of static website

First enter the /usr/local/nginx/confdirectory, you can see that there is a nginx.cnffile
Insert picture description here

Need to modify this file, you can vim nginx.confmodify it through . In order to facilitate the modification, the author adopts the editplusconnection virtual machine to modify, which is more convenient.
editplus official website: https://www.editplus.com/ , you can also download the cracked version yourself .
The connection steps are as follows:
Insert picture description here

After opening the ftp settings Insert picture description here
, click Add, enter the description, ip address, user password, and then click Advanced Options Insert picture description here
,, remove the check before using the default port, enter the port number (the default is also 22), and check Use sftp, click OK Return to the FTP setting interface and click the OK button.
Then open the directory drop-down box and click the remote connection just configured. Insert picture description here
After opening, the top is the directory, and the bottom is the file
.

Insert picture description here

Click to open the /usr/local/nginx/confdirectory, double-click to nginx.confopen it, and you can see the specific file content on the right.

Insert picture description here

Focus on the following
Insert picture description here

Root is the default access directory. This html directory is located in the /usr/local/nginx directory. Index is the file accessed by default, that is to say, directly enter the content accessed by ip, that is, the nginx access page. So we need to put the index directory (some static resources such as html and css, font, js, image, etc.) in our project under the /usr/local/nginx directory. Use xftp software or other methods.
xftp link: link: https://pan.baidu.com/s/1Y-RIWzSLlGSJzKgUr7b2Mg
extraction code: mxzz
Insert picture description here

After connecting, enter the /usr/local/nginxdirectory on the right , and drag the index directory on the left to the right.
Insert picture description here

Then change editplusthe rootcorresponding htmldirectory name in index.
Insert picture description here

Just click OK in the pop-up window.
Insert picture description here

Confirm again
Insert picture description here

Then enter the /usr/lcoal/nginx/sbindirectory,
Insert picture description here

Every time the computer restarts nginx after the computer is turned on, it will report an error
nginx: [error] open() “/usr/local/var/run/nginx.pid” failed (2: No such file or directory)

Insert picture description here

The reason for the error is that there is no nginx folder or nginx.pid file. The
reason is that every time the system restarts, the system will automatically delete the file, so you need to change the location of the pid file **

In the editplusopen nginx.confconfiguration file, the pre-pid # removed, and then save, then create logs directory under / usr / local / nginx directory

Insert picture description here
Insert picture description here

Then sbin/the next path ./nginx -c /usr/local/nginx/conf/nginx.confor perform ./nginx -s reloadreload the configuration file is automatically logsgenerated under nginx.pidthe file.
Insert picture description here

Finally, /usr/local/nginx/sbinexecute the ./nginxcommand in the directory to start nginx and enter ip access in the browser.

Insert picture description here

3.2 Configure virtual host

Virtual hosting, also called "website space", is to divide a physical server running on the Internet into multiple "virtual" servers. Virtual host technology has greatly promoted the application and popularization of network technology. At the same time, the rental service of virtual host has become a new economic form in the Internet age.

3.2.1 Port Binding

For example, there are multiple static page directories, and different directories need different ports, so you need to /usr/local/nginx/conf/nginx.confadd multiple ones server.

First upload the /usr/local/nginxdirectory where multiple static pages are located to the directory and modify the Nginx configuration file:
/usr/local/nginx/conf/nginx.conf
mainly modify listen and root, index. If it is configured in a different linux environment, you need to modify server_name

server {
        listen       80; # 监听的端口
        server_name  localhost; # 域名或ip
        location / {	# 访问路径配置
            root   index;# 根目录
            index  index.html index.htm; # 默认首页
        }
        error_page   500 502 503 504  /50x.html;	# 错误页面
        location = /50x.html {
            root   html;
        }
    }
     server {
        listen       81; # 监听的端口
        server_name  localhost; # 域名或ip
        location / {	# 访问路径配置
            root   regist;# 根目录
            index  regist.html; # 默认首页
        }
        error_page   500 502 503 504  /50x.html;	# 错误页面
        location = /50x.html {
            root   html;
        }   
    }

After modification, you can access the test after entering the /usr/local/nginx/sbindirectory and ./nginx -s reloadreloading the configuration file.

3.2.2 Domain name binding

What is a domain name:
Domain Name is the name of a computer or computer group on the Internet composed of a string of characters separated by "dots". It is used to identify the electronic location of the computer during data transmission (sometimes also referred to as Geographical location, geographic domain name, refers to a local area with administrative autonomy). A domain name is a "mask" on an IP address. The purpose of a domain name is to facilitate the memorization and communication of a group of server addresses (website, email, FTP, etc.). The domain name serves as the name of a memorable Internet participant within its power. Domain names are composed of the rules and procedures of the Domain Name System (DNS). Any name registered in DNS is a domain name. Domain names are used for specific naming and addressing purposes in various network environments and applications. Generally, a domain name represents an Internet Protocol (IP) resource, such as a personal computer used to access the Internet, a server computer hosting a website, or the website itself or any other service delivered over the Internet. The first registered domain name in the world was registered in January 1985.
Domain level:

  • Top-level domain names
    Top-level domain names are divided into two categories:
    one is national top-level domainnames (nTLDs). More than 200 countries have assigned top-level domain names according to ISO3166 country codes. For example, China is cn, the United States is us, and Japan. It is jp, etc.; the
    second is international top-level domain names (iTDs), such as .Com .Top for industrial and commercial enterprises, .net for network providers, .org for non-profit organizations, and education .edu, and unrestricted neutral domain names such as .xyz, etc. Most domain name disputes occur under the top-level domain name of com, because most companies go online for profit. However, due to the development of new top-level domain names since 2014, the number of domain name dispute cases has increased rapidly[5]. In order to strengthen domain name management and resolve the shortage of domain name resources, international organizations such as the Internet Association, Internet Sub-addressing Agency, and World Intellectual Property Organization (WIPO) have conducted extensive consultations to establish a new model based on the original three international generic top-level domains: (com). Added 7 international generic top-level domains: firm (company enterprises), store (sales companies or enterprises), Web (units highlighting WWW activities), arts (units highlighting cultural and entertainment activities), rec (highlighting pastimes and entertainment activities) ), info (units that provide information services), nom (individuals), and select new registration agencies around the world to accept domain name registration applications.
    For example: baidu.com
  • Second-level domain name
    Second-level domain name refers to the domain name under the top-level domain name. Under the international top-level domain name, it refers to the online name of the domain name registrant, such as ibm, yahoo, microsoft, etc.; under the national top-level domain name, it means the registered enterprise The symbol of the category, such as .top, com, edu, gov, net, etc.
    The top-level domain name officially registered and operated by China at the International Internet Network Information Center (Inter NIC) is CN, which is also China's first-level domain name. Under top-level domain names, China's second-level domain names are divided into category domain names and administrative region names. There are 7 category domain names, including ac for scientific research institutions; com and top for industrial and commercial financial enterprises; edu for educational institutions; gov for government departments; net for Internet Information Center and Operation Center; Org for non-profit organizations. There are 34 administrative regions, corresponding to provinces, autonomous regions, and municipalities in China.
    For example: map.baidu.com
  • Third-level domain name The
    third-level domain name is composed of letters (A~Z, a~z, uppercase and lowercase, etc.), numbers (0~9), and a hyphen (-). The domain names at all levels are connected by a solid dot (.). The length of the domain name cannot exceed 20 characters. If there is no special reason, it is recommended to adopt the applicant's English name (or abbreviation) or Chinese Pinyin name (or abbreviation) as the third-level domain name to maintain the clarity and conciseness of the domain name.
    For example:
    item.map.baidu.com
    domain name and IP binding:
    one domain name corresponds to one ip address, and one ip address can be bound by multiple domain names.
    When we enter the domain name address in the browser address bar, such as www.baidu.com, it will first go to C:\Windows\System32\drivers\etcthis directory to find the mapping relationship between the domain name and ip. If it finds the corresponding ip, it will go to the dns server.
    The mapping relationship between domain name and ip can be configured in the hosts file. If the corresponding relationship between domain name and ip is configured in the hosts file, there is no need to use the dns server. For local testing, you can modify the hosts file ( C:\Windows\System32\drivers\etc), add the following content
    ip address domain name,

    you can do the domain name pointing, you need to modify the nginx configuration file after the modification is completed
    server {
        listen       80;
        server_name  域名(如www.baidu.com);
        location / {
            root   index;
            index  index.html;
        }
    }
    

Then enter the /usr/local/nginx/sbindirectory and execute the following command to refresh the configuration file.
./nginx -s reload
If nginx is closed, execute again.
./nginx
Start nginx
and then enter in the address bar http://域名to access

4. Nginx reverse proxy and load balancing

4.1 Reverse proxy

4.1.1 What is a reverse proxy

Reverse Proxy means to use a proxy server to accept connection requests on the internet, then forward the request to the server on the internal network, and return the result from the server to the client requesting the connection on the internet. At this time, the proxy server acts as a reverse proxy server externally.
First, we first understand the forward proxy, as shown below:
Insert picture description here

The forward proxy is for your client, and the reverse proxy is for the server, as shown below

Insert picture description here

Insert picture description here

4.1.2 Configure reverse proxy-preparations

Deploy the project to tomcat and upload it to the server.

4.1.3 Configure reverse proxy

Modify the Nginx configuration file on the Nginx host

upstream tomcat-test{
    server 192.168.3.12:8080;
 }
 server {
     listen       80; # 监听的端口
     server_name  www.test.com; # 域名或ip
     location / {	# 访问路径配置
         # root   index;# 根目录
     proxy_pass http://tomcat-test.com;
         index  index.html index.htm; # 默认首页
     }
}

Restart Nginx and test with a browser: http://www.test.com( This domain name must be configured to point to ), refer to the domain name binding above.

4.2 Load balancing

4.2.1 What is load balancing

Load balancing is based on the existing network structure. It provides a cheap, effective and transparent method to expand the bandwidth of network equipment and servers, increase throughput, strengthen network data processing capabilities, and improve network flexibility and availability.
Load balancing, the English name is Load Balance, which means that it is distributed to multiple operating units for execution, such as Web servers, FTP servers, enterprise key application servers, and other mission-critical servers, so as to complete work tasks together.

4.2.2 Configure Load Balancing-Preparation

enterInsert picture description here

This directory, execute the following command
cp ./apache-tomcat-7.0.106 tomcat1
cp ./apache-tomcat-7.0.106 tomcat2
to /ur/local/apache-tomcat-7.0.106copy two copies, the apache-tomcat-7.0.106one in it server.xmldoes not need to be modified, and the ports of tomcat1 and tomcat2 must be modified (modified in its server.xml) tomcat1的serverl.xml中的所有涉及到的端口全部+1,tomcat2serverl.xml中的所有涉及到的端口全部+2.
Execute the following commands as the
cd t/usr/local/tomcat/tomcat1/bin
./startup.sh
other two, remember to rename tomcat1.
Start the three tomcat services separately.
In order to be able to distinguish which server's website is to be visited, a mark can be added to the title of the project's initial visit homepage for distinction. For example, <title>8080</title>
4.2.3 configure load balancing,
modify the Nginx configuration file:

  upstream tomcat-test {
	   server 192.168.3.12:8080;
	   server 192.168.3.12:8081;
	   server 192.168.3.12:8082;
    }
    server {
        listen       80; # 监听的端口
        server_name  www.test.com; # 域名或ip
        location / {	# 访问路径配置
            # root   index;# 根目录
	    proxy_pass http://tomcat-test.com;

            index  index.html index.htm; # 默认首页
        }
        error_page   500 502 503 504  /50x.html;	# 错误页面
        location = /50x.html {
            root   html;
        }
    }

Input in the address bar http:// www.test.com /Refresh and observe the title of each web page to see if it is different.
After testing, the probability of the appearance of the three servers is 33.3333333%, alternately displayed.
If one of the servers has better performance and you want it to bear more pressure, you can set the weight.
For example, if you want to make NO.1 appear twice as often as other servers, modify the configuration as follows:

    upstream tomcat-test {
	   server 192.168.3.12:8080;
	   server 192.168.3.12:8081 weight=2;
	   server 192.168.3.12:8082;
    }

After testing, every refresh four times, two times are 8081

Guess you like

Origin blog.csdn.net/m0_37959155/article/details/108913157