nginx proxy parameter proxy_pass implementation _nginx_ scripting home

Implementation of the nginx proxy parameter proxy_pass

Updated: Apr 29, 2024 11:25:53 by liujiangxu
The proxy_pass parameter is used to configure the reverse proxy. This paper mainly introduces the implementation of the nginx proxy parameter proxy_pass. It is introduced in detail through the example code, which has certain reference value for everyone's study or work

The proxy_pass parameter is used to configure the reverse proxy. It specifies that client requests are forwarded to the back-end server. The back-end address can be a domain name or an ip port URI

The proxy backend reports an error message indicating that the CSS file, JavaScript file, or image cannot be found locally

For example, nginx: 10.1.74.109 Back-end service: http://10.1.74.109:8082

Parameter configuration:

location /harbor {proxy_pass http://10.1.74.109:8082; }

在这里插入图片描述

Visit http://10.1.74.109/zabbix show is not complete, prompt the CSS file such as static file does not exist.

The reason is that proxy_pass does point to the back-end server, but when the browser loads the page, it may request some static resources, but these requests may not contain the /zabbix prefix, or the static resources may be dynamically generated, so it will look up these files locally

For example above the backend login before access address is http://10.1.74.109:8082/, have to address to http://10.1.74.109:8082/zabbix.php? after a successful login action=dashboard.view, without the /zabbix prefix

If the backend address suffix does not change the premise of the proxy, it is generally normal

Solution: Use proxy_set_header to set the correct Host header

location /zabbix {proxy_pass http://10.1.74.109:8082/; # The url must be followed by "/" proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; keepalive_timeout 500; }
argument implication
proxy_set_header Host $host; When nginx forwards a request, it sets the value of the Host request header to the Host name and port of the original request, and the back end may rely on the host header to determine what should be served
proxy_set_header X-Real-IP $remote_addr; X-Real-IP is used to set the real IP address of the requesting client, and $remote_addr is a variable containing the client's IP address
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; $proxy_add_x_forwarded_for is a special variable that contains the X-Forwarded-For header of the original request (if it exists) and the IP address of the client. The back-end server can see the IP addresses of all passing proxy servers and original clients.
keepalive_timeout 500; This section describes how to set a long connection timeout period. After a long connection is established between a client and a server, the connection remains open for the specified period. In this way, the client can send multiple requests through the same connection, reducing connection overhead and improving performance

Influence of different ways of writing proxy_pass

The address for requesting a client request is www.ljx.com/a.html

Method 1: proxy_pass http://10.1.1.1;

location /ceshi/ {proxy_pass http://10.1.1.1; ... }
  • Request address: www.ljx.com/ceshi/a.html
  • After the proxy address: http://10.1.1.1/ceshi/a.html
  • Explanation: The complete request URI (including /ceshi/a.html) will be sent to the backend server at 10.1.1.1. Since proxy_pass does not specify the URI part, the URI of the original request remains unchanged.

Method 2: proxy_pass http://10.1.1.1/;

location /ceshi/ {proxy_pass http://10.1.1.1/; ... }
  • Request address: www.ljx.com/ceshi/a.html
  • Address after proxy: http://10.1.1.1/a.html
  • Explanation: Since proxy_pass is followed by a slash /, nginx ignores the /ceshi/ part of the original request URI, leaving only the a.TML part and sending it to the back-end server

Method 3: proxy_pass http://10.1.1.1/index/;

location /ceshi/ {proxy_pass http://10.1.1.1/index/; ... }
  • Request address: www.ljx.com/ceshi/a.html
  • After the proxy address: http://10.1.1.1/index/a.html
  • Explanation: The /ceshi/ in the URI of the original request is replaced with /index/, which is then sent to the back-end server. The rest of the path a.html remains unchanged

Method 4: proxy_pass http://10.1.1.1/somepath;

location /ceshi/ {proxy_pass http://10.1.1.1/somepath; ... }
  • Request address: www.ljx.com/ceshi/a.html
  • Address after proxy: http://10.1.1.1/somepath
  • Explanation: Whatever the original request URI was, it will be completely replaced with the URI specified after proxy_pass (in this case /somepath). The query string (if any) is also ignored

This article about the implementation of the nginx proxy parameter proxy_pass article is introduced to this, more related to nginx proxy_pass content please search the previous articles of the script home or continue to browse the following related articles hope that you will support the script home in the future!

Related article

  • nginx代理参数proxy_pass的实现

    Implementation of the nginx proxy parameter proxy_pass

    The proxy_pass parameter is used to configure the reverse proxy. This paper mainly introduces the implementation of the nginx proxy parameter proxy_pass. It is introduced in detail through the example code, which has certain reference value for everyone's study or work
    2024-04-04
  • Nginx防止直接用IP访问Web服务器的设置方法

    Nginx prevents direct IP access to the Web server

    Read a lot of Nginx configuration, seems to ignore the ip direct access to the Web, which is not conducive to SEO optimization in theory, so we hope to avoid direct IP access to the website, but domain name access, how to do it, see below
    2012-09-09
  • 在Ubuntu20.04上安装Kubernetes集群

    Install the Kubernetes cluster on Ubuntu20.04

    Containerization provides a lot of flexibility for developers, one of the most commonly used containerized applications is Docker, one of the main reasons to deploy services on containers is that they are flexible, lightweight, and easy to scale when deployed on hundreds of machines, but who manages all these containers? This is where Kubernetes comes in
    2023-12-12
  • 在Linux和Windows系统上安装Nginx服务器的教程

    Tutorial for installing Nginx server on Linux and Windows systems

    This article mainly introduces the tutorial of installing Nginx server on Linux and Windows system,Linux system here is represented by CentOS, need friends can refer to the next
    2015-08-08
  • shell脚本之nginx自动化脚本

    nginx automation scripts for shell scripts

    Today Xiaobian will share an article about shell script nginx automation script for you, Xiaobian feel that the content is very good, now share with you, has a good reference value, need friends to follow Xiaobian to see
    2019-01-01
  • 在nginx中配置pathinfo模式支持thinkphp的URL重写

    Configuring pathinfo mode in nginx supports thinkphp URL rewriting

    This article mainly introduces the example of configuring pathinfo mode in nginx, which is used to support thinkphp URL rewriting, and solves thinkphp a path problem, which needs friends to refer to
    2014-04-04
  • Nginx反向代理学习实例教程

    Nginx Reverse proxy learning example tutorial

    nginx as a web server an important function is the reverse proxy, of course, you can also use nginx configuration forward proxy, this article mainly introduces you about Nginx reverse proxy related information, need friends can refer to
    2021-10-10
  • 关于nginx没有跳转到upstream地址的解决

    Fix about nginx not jumping to upstream address

    This article mainly introduces the solution that nginx does not jump to the upstream address. The article introduces it in great detail through the example code, which has certain reference value for everyone's study or work. The friends who need it will learn together with Xiaobian below
    2019-09-09
  • 教你nginx跳转配置的四种方式

    Teach you four ways to jump nginx configuration

    Nowadays, with the increase of application services, services may be deployed on different servers, the following article mainly introduces the four ways of nginx jump configuration, the need of friends can refer to
    2022-07-07
  • Nginx 实现灰度发布的三种方法总结

    Summary of three methods to implement gray scale publishing in Nginx

    This article mainly introduces the three methods of Nginx gray scale release summary of the relevant information, the need of friends can refer to the next
    2017-05-05

Latest comments