Nginx construction and use (linux)

Table of contents

1. Concept

2. Tool download

3. Unzip and install

         1. Unzip and install the pcre-8.40.tar.bz2 file

        2. Unzip and install the zlib-1.2.11.5.tar.gz file

        3. Unzip and install the openssl-1.0.1t.tar.gz file

        4. Unzip and install the nginx-1.10.1.tar.gz file


1. Concept

        Nginx is a high-performance HTTP and reverse proxy server, which can be used to handle static files, load balancing, reverse proxy and other functions.
        The origin of Nginx is this: it was developed by the Russian Igor Sysoyev for the Rambler.ru site, and the first public version was released on October 4, 2004.
        The advantages of Nginx are:
                 1. Simple configuration and easy maintenance
                 2. High static processing performance, less memory usage, and high concurrency
                 3. Support multiple load balancing strategies, such as polling, weight, IP binding, etc.
                 4. Support hot deployment , uninterrupted service
                 5. The disadvantages of multi-language general server
        Nginx are:
                1. The processing of dynamic requests needs to cooperate with other back-end servers, such as PHP-FPM, Tomcat, etc.
                2. There are relatively few modules, not as rich as Apache
                3. Yes Windows support is not so good

        Forward proxy means that the client accesses the external network through a proxy server, and the proxy server forwards the client's request to the target server and returns the obtained content to the client. The forward proxy can hide the real identity of the client and provide access control, caching and other functions for the client.
        Reverse proxy means that the external network accesses the internal network through a proxy server, and the proxy server forwards the request to the backend server and returns the obtained result to the external network. The reverse proxy can hide the real identity of the back-end server and provide load balancing, security protection, caching and other functions for the back-end server.

The workflow         of Nginx as a reverse proxy server :

                1. The client sends a request to the server, connects to the server, the user does not know the server address, only the address of the reverse proxy server is public 2. The
                request is sent directly to the reverse proxy server
                3. The reverse proxy server forwards the request to the following Web server web server N reverse proxy servers will poll for forwarding requests
                4. The web server receives the request for processing and gets the result
                5. The web server sends the processing result to the reverse proxy server
                6. The reverse proxy server will get The results are forwarded to the client

2. Tool download

        The tools needed to use Nginx are pcre library, zlib library, OpenSSL library

             pcre library download address: here

             zlib library download address: here

             OpenSSL library download address: here

             Nginx download address: here

After downloading these packages, they need to be uploaded to the server for decompression and download

3. Unzip and install

        Take the following packages as an example

         1. Unzip and install the pcre-8.40.tar.bz2 file

顺序执行以下命令
a.	tar -jxvf pcre-8.40.tar.bz2
b.	cd pcre-8.40/
c.	./configure
d.	make
e.	sudo make install

        2. Unzip and install the zlib-1.2.11.5.tar.gz file

顺序执行以下命令
a.	tar -zxvf zlib-1.2.11.tar.gz
b.	cd zlib-1.2.11/
c.	./configure
d.	make
e.	sudo make install

        3. Unzip and install the openssl-1.0.1t.tar.gz file

顺序执行以下命令
a.	tar -zxvf openssl-1.0.1t.tar.gz
b.	cd openssl-1.0.1t/
c.	./config
d.	make
e.	sudo make install

        4. Unzip and install the nginx-1.10.1.tar.gz file

顺序执行以下命令
a.	tar -zxvf nginx-1.10.1.tar.gz
b.	cd nginx-1.10.1/
c.	./configure
d.	make
e.	sudo make install

after decompression

Note: When installing the nginx file, if you use it to work, you need to install dependencies at the same time, use the following command

#指定了安装目录,以及依赖
sudo ./configure --sbin-path=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.40 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.1t

指定了安装目录在 /usr/local/nginx 下,以及启动文件 nginx 也在该目录下

directory after installation

 When executing the make or make install command, if all warnings being treated as errors appears , it means that the warning information is regarded as an error message, and the following command can be used

make -no-warings-are-errors
sudo make install -no-warings-are-errors
  • nginx installation can also use the apt-get command
#使用apt-get命令安装
sudo apt-get install nginx
# nginx文件安装完成之后的文件位置:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志
  • nginx startup and shutdown commands
#查看nginx的启动转态
ps aux|grep nginx
#启动命令,需要进入到/usr/local/nginx文件下使用
sudo ./nginx
#重新加载
sudo ./nginx -s reload
#停止命令
sudo ./nginx -s stop

#以下是使用yum命令安装时使用的命令
# 要停止Nginx服务,请运行:
sudo systemctl stop nginx
# 要再次启动,请键入:
sudo systemctl start nginx
# 重新启动Nginx服务:
sudo systemctl restart nginx
# 在进行一些配置更改后重新加载Nginx服务:
sudo systemctl reload nginx
# 如果你想禁用Nginx服务在启动时启动:
sudo systemctl disable nginx
# 并重新启用它:
sudo systemctl enable nginx
  • nginx configuration file, the command to install dependencies and paths above is used here, and the tar.gz file needs to be downloaded and uploaded to the server.
user www-data;    #所属用户(权限),可以设置为root
worker_processes auto;    #设置连接的最大线程数
pid /run/nginx.pid;        #在 /run/ 目录下生成pid文件
include /etc/nginx/modules-enabled/*.conf;

# nginx的事件处理
events {    
    use epoll;    #添加该行表示多路IO转接模型使用epoll
	worker_connections 768;    #最大连接数
	# multi_accept on;
}

http {

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	include /etc/nginx/mime.types;
	default_type application/octet-stream;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;
	gzip on;
	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;

    server{
        listen         80;         #绑定的端口号
        server_name    localhost;  #设定的域名
        charset        utf8;       #编码
        # \ 表示一个指令,相当于 \index.html
        location \ { #处理客户端的请求
            root html; #表示查找的根目录,都是相对于 /usr/local/nginx 目录
           }
        # \hello 表示一个目录
        location \hello\ {
            root html;
           }
        location \upload\ {
            root html;
            index upload.html;#默认打开的网页
           }       
}

Configure an instance as a web server

  • as a reverse proxy server

There are three roles:

        1. Client: use the browser as the client

        2. Reverse proxy server: nginx configuration file under windows

        3. web server: use the nginx server configured in ubuntu above

  ->Configure nginx file under windows

You need to download the nginx package on the official website, any one of the following will do

extract it

 Right-click the mouse in the current directory -> open the terminal, enter the following command to start the server

Configure the direction proxy server file, enter the conf directory, open the nginx.conf file, and add the following content

server {#需要代理几台服务器就需要添加几个server模块
    listen       80;	#客户端反向代理服务器,代理服务器监听的端口
    server_name  test.com;	#域名

location / {
	#反向代理服务器转发指令, http:// 固定
    proxy_pass http://robin.test.com;
   }

   }
#添加一个代理模块
upstream robin.test.com
{
	server 192.168.71.128:80;
}

Specific content

The directory where the local host is located -> C:\Windows\System32\drivers\etc, you need to use the administrator to open the host file

The configuration content is as follows:

  • load balancing configuration
    反向代理的中间产物
    server{
    	listen 80;
    	server_name test1.com;
    	location \ {
    		porxy_pass http://test1.com;
    	}
    }
    upstream test1.com{
    	server 192.168.247.135:80;
    }
    server{
    	listen 80;
    	server_name test2.com;
    	location \ {
    		porxy_pass http://test2.com;
    	}
    }
    upstream test2.com{
    	server 192.168.247.250:80;
    }
    反向代理变成负载均衡配置项
    server{
    	listen 80;
    	server_name localhost;
    	location \ {
    		proxy_pass http://linux.com
    	}
    }
    upstream linux.com{
    	server 192.168.247.135:80 weight=2;
    	server 192.168.247.250:80 weight=1;
    }

Guess you like

Origin blog.csdn.net/weixin_62859191/article/details/130277443