Basic introduction to nginx (installation, common commands, reverse proxy)

introduction

In the Internet world, nginx(pronounced " engine-x") has become a popular and used open source high-performance HTTPserver and reverse proxy server. It has become the first choice for many websites and applications due to its ease of use, high efficiency and stability. This article will introduce you nginxto the basic concepts and functions in detail, including how to install nginx, commonly used commands, and how to use it nginxto implement reverse proxy. Whether you are a novice nginxor a familiar nginxdeveloper, this article will provide you with valuable content and practical tips. Let us learn more about it nginxand discover its charm!

1. What is nginx

NginxIt is an open source high-performance HTTPand reverse proxy server that can also be used as a mail proxy server and a general TCP/UDPproxy server. Its design focuses on high concurrency, high performance and low memory consumption, and is often used to provide static content, load balancing and as Weba server.

NginxHas the following functions and features:

  1. Static file service: As a web server, Nginxit can handle requests for static files, and provide better performance by quickly processing requests and sending static content.

  2. Reverse proxy: NginxIt can be used as a reverse proxy server to forward client requests to multiple back-end servers to provide functions such as load balancing, caching and security.

  3. Dynamic content handling: Although primarily intended for handling static files, it can also be used Nginxwith various backend applications (such as etc.) for handling dynamic content.PHP、Python、Java

  4. High performance: NginxIt adopts an asynchronous, non-blocking event-driven architecture, which can handle a large number of concurrent connections and provide excellent performance.

  5. Low memory consumption: NginxWhen handling a large number of concurrent connections, it uses less memory, so it can provide a higher number of concurrent connections and better performance.

  6. Flexible configuration: NginxThe configuration is very flexible, and administrators can perform various configurations and optimizations according to needs to meet different needs.

All in all, Nginxit is a high-performance, flexible and scalable web server and proxy server, suitable for various scenarios, including static file serving, reverse proxy, load balancing and dynamic content processing, etc.

2. Download and install nginx

1. Download

nginx download address
insert image description here
Go to the official website to download the corresponding system version

2. Install under windows

Download the compressed zip package and use it directly after decompression
insert image description here
Find the installation directory
insert image description here
and execute

ngnix -v

Check the version number,
insert image description here
you can see the version number means the installation is successful

3. run

Double-click the nginx.exe file to start nginx and
insert image description here
access it in a local browser. 127.0.0.1If you see this interface, it means the startup is successful.
Here I changed the port number to 8088, and the default is 80.
insert image description here

4. The external server cannot be accessed

After ensuring that nginx on the server is started normally, the external server cannot be accessed, probably because the port is not open to the outside world.
At this point we need to go to the server to configure入站规则

Here is an example of Aliyun server:
open the console, find the security group, configure the rule
insert image description here
entry-manually add
insert image description here
the configuration port, the default is 80.
If you have not changed the port in your nginx configuration, you can configure 80/80 here to
insert image description here
configure The accessible ip
0.0.0.0/0 means that all can access this port of your server, and you can also specify which ip can be accessed according to the specific situation.
insert image description here
After the configuration is completed , save it, and
then go to the browser to access the public network ip of your server . If the display is as follows, it means that the port development is successful.
insert image description here

3. Common commands of nginx

Order explain
nginx -v Display nginx version information
nginx -t Check whether the syntax of the nginx configuration file is correct
nginx -s start Start the nginx service
nginx -s stop stop nginx service
nginx -s reload Reload the nginx configuration file
nginx -s quit Gracefully stop nginx service
nginx -s reopen Restart all open log files
nginx -s reload -squit Reloads nginx configuration files and gracefully stops old worker processes
nginx -s stop -wait Stop the nginx service and wait for the old worker process to exit
nginx -c file Specifies the configuration file to use
nginx -g directives Set global configuration directives on the command line
nginx -h show help information
nginx -p prefix Set the prefix path (this path will be used when looking for configuration files)
nginx -q Turn off error output when checking configuration
nginx -V Show configuration options for nginx
nginx -s reopen Restart all open log files
nginx -s reload -squit Reloads nginx configuration files and gracefully stops old worker processes
nginx -s stop -wait Stop the nginx service and wait for the old worker process to exit
nginx -c file Specifies the configuration file to use
nginx -g directives Set global configuration directives on the command line

Note that some commands may need to be run with administrator privileges, otherwise they may not execute successfully.

4. nginx.config

nginx.configYes nginxconfiguration files, used to configure nginxvarious parameters and options of the server.

In general, nginx.configconfiguration options can be broken down into the following sections:

  1. Global configuration options :

    • worker_processes: Specifies the number of worker processes to handle client requests.
    • error_log: Specifies the path and level of the error log file.
    • events: Sets the event model, including the event driver used and the number of connections.
    • http: Specify http-related configuration options.
  2. http configuration options :

    • server: Configure the virtual host, specify the listening IP address and port number.
    • location: Configure request matching rules and processing methods.
    • proxy_pass: Configure the reverse proxy to forward the request to the specified backend server.
    • root: Specifies the root directory for static files.
    • index: Specifies the default index file.
    • try_files: Specifies the rules for finding files.

For example, the following is an example of a simple nginx.config configuration file:

worker_processes  1;
error_log  /var/log/nginx/error.log;
events {
    
    
    worker_connections  1024;
}
http {
    
    
    server {
    
    
        listen  80;
        server_name  example.com;
        location / {
    
    
            root  /var/www/html;
            index  index.html index.php;
        }
        location /api/ {
    
    
            proxy_pass  http://api.example.com;
        }
    }
}

In the above configuration, a workerprocess is specified, the path and level of the error log are set, and the number of connections for the event is set.
In httpthe section, a virtual host is defined, the listening 80port, the matching rule is the root path and /api/the path.
The root path is configured with the root directory and the default index file, and /api/the path is configured with a reverse proxy to the backend server.

五、With FileZ

1. What is FileZilla

FileZilla is a free and open source FTP software for uploading and downloading files to a server . It serves the following purposes:

  1. File transfer: Through FTP, FTPS and SFTP protocols, files can be uploaded from the local computer to the server or downloaded from the server to the local computer.

  2. File Management: Files can be browsed, edited, copied, moved and deleted between the local computer and the server.

  3. Website maintenance: It can be used to manage website files on remote servers, and can easily update website content, add or delete files and folders.

  4. Automation: Supports file transfer queues and batch operations, can set up scheduled tasks, automatically upload or download files, and perform automated website backups.

  5. Cross-platform use: It can run on multiple operating systems such as Windows, Mac OS and Linux.

In conclusion, FileZilla is a powerful file transfer and management tool for webmasters and developers who need frequent file operations.

2. Download and install FileZilla

FileZilla download address
insert image description here

Select the corresponding version to download.
After the windows version is downloaded, follow the prompts to install it step by step.
After the installation is successful, fill in the remote host ip, user, password, port to link
insert image description here

6. Reverse proxy

1. What is nginx reverse proxy

Nginx's reverse proxy means that Nginx acts as a proxy server and forwards client requests to the back-end server. The reverse proxy hides the real IP address of the back-end server, improves the security of the server, and can distribute requests to multiple back-end servers in a load-balanced manner.

2. Reverse proxy workflow

  1. The client sends a request to the Nginx server.
  2. After receiving the request, the Nginx server selects a back-end server to process the request according to preset rules and algorithms.
  3. The Nginx server forwards the request to the selected backend server.
  4. Backend servers process requests and generate responses.
  5. The backend server returns the response to the Nginx server.
  6. The Nginx server returns the received response to the client.

The following is a description of a symbolic image:

客户端             Nginx服务器             后端服务器
  |                      |                          |
  |  --------请求-------->|                          |
  |                      |                          |
  |  <-------响应--------|                          |
  |                      |                          |
  |            -----------选择后端服务器----------->
  |                      |                          |
  |            <---------发送请求到后端服务器---------
  |                      |                          |
  |                      |---处理请求并生成响应------>|
  |                      |                          |
  |                      |<------返回响应------------|
  |                      |                          |
  |            <---------返回响应到Nginx服务器---------
  |                      |                          |
  |  <--------返回响应-----|                          |
  |                      |                          |

Through this reverse proxy method, Nginx can provide functions such as load balancing, high availability, and caching to optimize server performance and stability.

3. How to configure nginx reverse proxy

In the nginx.conf configuration file, you can use locationdirectives to configure the path and target server for the reverse proxy. The following is an example configuration that proxies all requests from the local port 8080 to the target server example.com.

http {
    
    
    server {
    
    
        listen 80;

        location / {
    
    
            proxy_pass http://example.com;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

In the above configuration, proxy_passthe directive forwards the request to http://example.com. proxy_set_headerDirectives are used to set HTTP headers that pass the request's original host and client IP address to the target server.

locationMultiple reverse proxy paths can be configured as needed using directives. locationDifferent values ​​can be set for each block proxy_passto specify different target servers. For example, in the following configuration, proxy the path /apito http://api.example.com, /appproxy the path to http://app.example.com.

http {
    
    
    server {
    
    
        listen 80;

        location /api {
    
    
            proxy_pass http://api.example.com;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }

        location /app {
    
    
            proxy_pass http://app.example.com;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

It should be noted that the above examples only show the basic reverse proxy configuration. According to actual needs, you may need to add other configuration parameters, such as load balancing, caching, SSLand so on. For detailed configuration, please refer to nginxthe official documentation.

Every time you modify nginx.configthe file, enter:

nginx -s reload

Reload the configuration file.

Summarize

Well, in this note, we nginxgave a basic introduction to , and learned how to install, common commands, and how to configure reverse proxy. NginxAs a high-performance web server and reverse proxy server, it has excellent performance and stability and is widely used in the Internet field. Whether it is a web server or a reverse proxy server, nginxit provides rich functions and flexible configuration options, enabling us to customize according to actual needs. I hope that through the introduction of this article, you have nginxa deeper understanding of the technology and can use it flexibly in practical applications. If you have any questions or doubts, please leave a message to communicate. Thanks for reading!

Guess you like

Origin blog.csdn.net/jieyucx/article/details/132444462