📜  nginx proxy_pass (1)

📅  最后修改于: 2023-12-03 15:03:10.699000             🧑  作者: Mango

Nginx Proxy_pass

When it comes to web servers, Nginx is one of the most popular choices among developers. One of the many features that make Nginx a great choice as a web server is its ability to act as a reverse proxy.

In Nginx, the proxy_pass directive is used to define the reverse proxy configuration. This directive allows you to specify the URL of the upstream server that will handle requests for the current location.

Syntax
proxy_pass URL;

Where URL is the address of the upstream server. This address can be either a domain name or an IP address.

Examples
Simple Reverse Proxy

Let's start with a simple example. Consider the following configuration:

location / {
    proxy_pass http://app_server;
}

In this example, app_server is the DNS name of the upstream application server. Any requests that come to Nginx's root location will be passed on to the app_server.

Load Balancing

Nginx's proxy_pass directive can also be used to implement load balancing for multiple upstream servers. Consider the following example:

upstream app_servers {
    server 10.1.1.1:8080;
    server 10.1.1.2:8080;
    server 10.1.1.3:8080;
}

location / {
    proxy_pass http://app_servers;
}

In this example, we have defined an upstream group of three application servers, and all requests coming to Nginx's root location will be load-balanced across the three servers using a round-robin algorithm.

SSL Termination

The proxy_pass directive can also be used to implement SSL termination on Nginx. Consider the following example:

location / {
    proxy_pass https://app_server;
    proxy_ssl_verify off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
}

In this example, we have configured Nginx to pass on SSL encrypted traffic to the app_server. We have also added some additional headers that the application server may need for logging purposes.

Conclusion

The proxy_pass directive in Nginx is a powerful tool that allows developers to implement reverse proxy configurations, load balancing, and SSL termination. With its flexibility and ease of use, Nginx continues to be a top choice for web servers among developers.