This is the 7th day of my participation in Gwen Challenge

Nginx is introduced

Nginx is a high-performance HTTP/reverse proxy server and E-mail (IMAP/POP3) proxy server. Developed by Russian programmer Igor Sysoev, Nginx was officially tested to support up to 50,000 concurrent links with very low CPU, memory and other resource consumption.

Nginx application scenarios:

1. HTTP server. Nginx is an HTTP service that can provide HTTP services independently. Can do web static server.

2. Virtual host. Can be realized in a server virtual out of multiple websites. For example, the virtual host used by personal websites.

3. Reverse proxy, load balancing. When the number of visits to the website reaches a certain level, a single server cannot meet the user’s request, you need to use multiple server cluster can use Nginx as a reverse proxy. In addition, multiple servers can evenly share the load, so that a server is not idle due to the high load of a server downtime.

Environment to prepare

The following environments must be installed on the VIRTUAL machine.

The environment of GCC.

yum install gcc-c++

PCRE(Perl Compatible Regular Expressions) is a Perl library that includes the Perl-compatible Regular expression library. Nginx’s HTTP module uses PCRE to parse regular expressions, so you need to install the PCRE library on Linux. Pcl-devel is a secondary development library developed using PCRE. Nginx also needs this library.

yum install -y pcre pcre-devel

The Zlib library provides a variety of compression and decompression methods. Nginx uses Zlib to gzip the contents of HTTP packages, so you need to install zlib on Linux.

yum install -y zlib zlib-devel

OpenSSL is a powerful Secure Socket layer cryptographic library that includes major cryptographic algorithms, common key and certificate encapsulation management capabilities, and SSL protocols, and provides rich applications for testing and other purposes. Nginx supports both HTTP and HTTPS (that is, HTTP over SSL), so you need to install the OpenSSL library on Linux.

yum install -y openssl openssl-devel

Nginx installation

Official website: nginx:nginx.org/

The version we will use in this course is version 1.8.0.

Upload the Nginx package and decompress tar ZXVF nginx-1.8.0.tar.gz

Go to the nginx-1.8.0 directory and create the makeFile using the following 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/scgiCopy the code

Science:

A Makefile is a configuration file. The source files in a project are not counted. They are stored in several directories by type, function, and module. Even more complex functions can be performed because a Makefile is like a Shell script that can execute operating system commands.

--prefix=/usr \ points to the installation directory --sbin-path=/usr/sbin/nginx \ points to the program file (nginx). --conf-path=/etc/nginx/nginx.conf \ Points to the configuration file --error-log-path=/var/log/nginx/error.log \ points to the log - HTTP - log - path = / var/log/nginx/access. Log \ to HTTP - log - pid - path = / var/run/nginx/nginx. Pid \ to pid --lock-path=/var/ lock-nginx. lock \ (Install file lock, prevent install file from being used by others, or own misoperation.) --user=nginx \ --group=nginx \ --with-http_ssl_module \ enable ngx_http_SSL_module support Openssl must be installed) --with-http_flv_module \ Enable ngx_http_FLv_module support (provides memory seeking using time-based offset files) --with-http_stub_status_module \ Enable ngx_http_stub_status_module support (get the working status of nginx since last startup) -- with-http_gzip_STATIC_module \ Enable ngx_http_gzip_STATIC_module support (online real-time compressed output streams) --http-client-body-temp-path=/var/tmp/nginx/client/ \ Set the HTTP client request temporary file path --http-proxy-temp-path=/var/ TMP /nginx/proxy/ \ Set HTTP proxy temporary file path --http-fastcgi-temp-path=/var/ TMP /nginx/ fcgi/\ Set HTTP proxy temporary file path --http-fastcgi-temp-path=/var/ TMP /nginx/ fcgi/\ Fastcgi temporary file path --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ HTTP uwsgi temporary file path --http-scgi-temp-path=/var/tmp/nginx/scgi \ Set HTTP SCgi temporary file path --with-pcre Enable pcRE libraryCopy the code

Compiler: make

Install: make install

/var/temp/nginx/client /var/temp/nginx/client

mkdir /var/temp/nginx/client -p

Go to the sbin directory under the Nginx directory

cd /usr/local/nginx/sbin

Enter the command to start Nginx

./nginx

Check the process

ps aux | grep nginx

Enter the IP address of the virtual machine in the browser (port 80 by default, which means nothing is written).

If you cannot access it, it is generally a firewall problem.

Disable the default CentOS7 firewall:

systemctl stop firewalld.service
systemctl disable firewalld.service
Copy the code

If not, try local access in CentOS7.

$curl http://127.0.0.1

Can see this page code, basically is the server firewall problem, need to check carefully.

Close the nginx:

./nginx -s stop

Or./nginx -s quit

Restart nginx :(mainly used to refresh configuration files)

./nginx -s reload

Static Site deployment

Static page deployment

Upload all the contents of the static page you want to display to /usr/local/nginx/html on the server.

According to the analysis, it can be found that the tag under the server is used to configure the website access.

Since my static resources are in the HTML/Hankdog folder, I changed the root path.

Index Indicates the name of the home page.

Configuring a Virtual Host

Virtual hosting, also known as “web space”, is a physical server running on the Internet divided into multiple “virtual” servers. Virtual host technology has greatly promoted the application and popularization of network technology. At the same time the virtual host rental service has become a new economic form in the network era.

Port binding

To distinguish between different path sites by different port numbers, you have two static sites that can be distinguished by port numbers.

Such as:

server { listen 81; Server_name localhost; # root index; # root directory index index.html index.htm; } error_page 500 502 503 504/50x.html; # error page location = /50x.html {root HTML; } } server { listen 82; Server_name localhost; [rootregist] [rootregist] # root directory index regist.html; } error_page 500 502 503 504/50x.html; # error page location = /50x.html {root HTML; }}Copy the code

Enter http://ip/:81 in the address box and http://ip/:82 in the address box

Domain name binding

A domain name corresponds to an IP address, and an IP address can be bound by multiple domain names.

For local tests, you can modify the hosts file directly

(C: \ Windows \ System32 \ drivers \ etc.)

You can configure the mapping between domain names and IP addresses. If the mapping between domain names and IP addresses is configured in the hosts file, you do not need to use the DNS server.

Add 192.168.160.145 www.xn2001.com 192.168.160.145 test.xn2001.com to the end

After the domain name pointing, modify the nginx configuration file.

Change the mapping of server_name to your domain name.

The reverse proxy

In Reverse Proxy mode, a Proxy server receives Internet connection requests, forwards the requests to the Intranet server, and returns the results to the Internet client. In this case, the proxy server acts as a reverse proxy server.

First let’s understand the forward proxy (generally called proxy), as shown below:

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

To put it more simply, instead of directly accessing the Tomcat server, we let Nginx do it for us. What Nginx does is called a reverse proxy.

Configuring a Reverse Proxy

(1) Deploy your case to Tomcat (ROOT directory) and upload it to the server.

(2) Start TOMCAT and enter http://ip:8080 to view the homepage and configure the reverse proxy

There are two main configuration points, first configure a name + IP: port, and then implant in our server.

Upstream tomcat {server 192.168.160.145:8080; } # apply it to server {listen 80; server_name www.xn2001.com; location / { proxy_pass http://tomcat; # for the reverse proxy above, call.index index.html; }}Copy the code

Restart Nginx and test with your browser: www.xn2001.com (this domain name must be configured to point to the domain name)

Load balancing

Load balancing: Based on the existing network structure, it provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase throughput, enhance network data processing capacity, and improve the flexibility and availability of the network.

Load Balance is distributed among multiple operation units, such as the Web server, FTP server, enterprise critical application server, and other critical task servers, to jointly complete work tasks.

Configure load balancing – preparations (1) make three copies of tomcat for storing the project and change the ports to 8080 and 8081,8082 respectively

(2) Start the three Tomcat services respectively.

(3) in order to be able to distinguish which server to visit the website, you can add marks in the home title and so on in order to distinguish.

Try visiting first

http://192.168.160.145:8080/ http://192.168.160.145:8081/ http://192.168.160.145:8082/

Configuring Load Balancing

Upstream name reverse proxy (upstream name reverse proxy

Restart the Nginx server and test it in your browser.