Home >Operation and Maintenance >Linux Operation and Maintenance >How to configure a reverse proxy (like Nginx) on Linux

How to configure a reverse proxy (like Nginx) on Linux

WBOY
WBOYOriginal
2023-07-06 14:28:393583browse

How to configure a reverse proxy (such as Nginx) on Linux

Introduction:
In modern Internet applications, reverse proxy is a common component used to provide high performance and scalability . Nginx is a popular open source software that can be used as a reverse proxy server. This article will introduce how to configure Nginx as a reverse proxy server on Linux and provide code examples.

1. Install Nginx

First, we need to install Nginx. In most Linux distributions, Nginx can be quickly installed using a package manager. Here are examples of installation commands for several common package managers:

For Debian and Ubuntu distributions:
sudo apt update
sudo apt install nginx

For CentOS and Fedora distributions Version:
sudo yum install epel-release
sudo yum install nginx

After the installation is complete, you can use the following command to verify whether Nginx has been installed correctly:

sudo systemctl status nginx

If the status shows "active (running)", it means that Nginx has been successfully installed and is running.

2. Configure reverse proxy

In the Nginx configuration file, we can edit the main configuration file located at "/etc/nginx/nginx.conf" to configure the reverse proxy.

Open the file and find the section starting with "http {", and then add the following configuration code at the end of the section:

server {
listen 80;
server_name example. com;

location / {

proxy_pass http://webserver;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;

}
}

In the above example, "example.com" is your domain name or IP address, you can Replace with actual value. In the "proxy_pass" directive, we used "http://webserver" as the address of the backend server. You can replace this with your actual backend server address.

After saving the configuration file, reload the Nginx configuration:

sudo systemctl reload nginx

At this time, Nginx has been configured as a reverse proxy server.

3. Verify reverse proxy

To verify whether Nginx's reverse proxy is working properly, you can test it by accessing the configured domain name or IP address in the browser.

Enter "http://example.com" in the browser, you will see that Nginx successfully proxies the request to the backend server and returns its response to the browser.

During access to the reverse proxy, you can check whether the requested IP address is displayed correctly in the backend server's log file. This is achieved via the "proxy_set_header X-Real-IP $remote_addr;" directive.

Conclusion:
Through the guide in this article, you have learned how to configure Nginx as a reverse proxy server on Linux. By taking full advantage of Nginx's reverse proxy capabilities, your application can achieve higher performance and scalability. I wish you a happy use!

The above is the detailed content of How to configure a reverse proxy (like Nginx) on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn