Article Directory
introduction
In the Internet world, nginx
(pronounced " engine-x
") has become a popular and used open source high-performance HTTP
server 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 nginx
to the basic concepts and functions in detail, including how to install nginx
, commonly used commands, and how to use it nginx
to implement reverse proxy. Whether you are a novice nginx
or a familiar nginx
developer, this article will provide you with valuable content and practical tips. Let us learn more about it nginx
and discover its charm!
1. What is nginx
Nginx
It is an open source high-performance HTTP
and reverse proxy server that can also be used as a mail proxy server and a general TCP/UDP
proxy 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 Web
a server.
Nginx
Has the following functions and features:
-
Static file service: As a web server,
Nginx
it can handle requests for static files, and provide better performance by quickly processing requests and sending static content. -
Reverse proxy:
Nginx
It 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. -
Dynamic content handling: Although primarily intended for handling static files, it can also be used
Nginx
with various backend applications (such as etc.) for handling dynamic content.PHP、Python、Java
-
High performance:
Nginx
It adopts an asynchronous, non-blocking event-driven architecture, which can handle a large number of concurrent connections and provide excellent performance. -
Low memory consumption:
Nginx
When handling a large number of concurrent connections, it uses less memory, so it can provide a higher number of concurrent connections and better performance. -
Flexible configuration:
Nginx
The configuration is very flexible, and administrators can perform various configurations and optimizations according to needs to meet different needs.
All in all, Nginx
it 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
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
Find the installation directory
and execute
ngnix -v
Check the version number,
you can see the version number means the installation is successful
3. run
Double-click the nginx.exe file to start nginx and
access it in a local browser. 127.0.0.1
If you see this interface, it means the startup is successful.
Here I changed the port number to 8088, and the default is 80.
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
entry-manually add
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
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.
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.
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.config
Yes nginx
configuration files, used to configure nginx
various parameters and options of the server.
In general, nginx.config
configuration options can be broken down into the following sections:
-
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.
-
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 worker
process is specified, the path and level of the error log are set, and the number of connections for the event is set.
In http
the section, a virtual host is defined, the listening 80
port, 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:
-
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.
-
File Management: Files can be browsed, edited, copied, moved and deleted between the local computer and the server.
-
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.
-
Automation: Supports file transfer queues and batch operations, can set up scheduled tasks, automatically upload or download files, and perform automated website backups.
-
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
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
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
- The client sends a request to the Nginx server.
- After receiving the request, the Nginx server selects a back-end server to process the request according to preset rules and algorithms.
- The Nginx server forwards the request to the selected backend server.
- Backend servers process requests and generate responses.
- The backend server returns the response to the Nginx server.
- 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 location
directives 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_pass
the directive forwards the request to http://example.com
. proxy_set_header
Directives are used to set HTTP headers that pass the request's original host and client IP address to the target server.
location
Multiple reverse proxy paths can be configured as needed using directives. location
Different values can be set for each block proxy_pass
to specify different target servers. For example, in the following configuration, proxy the path /api
to http://api.example.com
, /app
proxy 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, SSL
and so on. For detailed configuration, please refer to nginx
the official documentation.
Every time you modify nginx.config
the file, enter:
nginx -s reload
Reload the configuration file.
Summarize
Well, in this note, we nginx
gave a basic introduction to , and learned how to install, common commands, and how to configure reverse proxy. Nginx
As 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, nginx
it 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 nginx
a 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!