Common Nginx proxy configuration

Source: Internet
Author: User

Common Nginx proxy configuration

Due to business system requirements, the web service must be used as an nginx proxy. During constant attempts, the common nginx proxy configuration is briefly summarized.

1. Simplest reverse proxy configuration

Under the http node, configure the service address using upstream and configure the proxy ing using the location of the server.

upstream my_server {                                                             server 10.0.0.2:8080;                                                    keepalive 2000;}server {    listen       80;                                                             server_name  10.0.0.1;                                                   client_max_body_size 1024M;    location /my/ {        proxy_pass http://my_server/;        proxy_set_header Host $host:$server_port;    }}

With this configuration, requests accessing the nginx address http: // 10.0.0.1: 80/my will be forwarded to the my_server service address http: // 10.0.0.2: 8080 /.

Note that, if the configuration is as follows:

upstream my_server {                                                             server 10.0.0.2:8080;                                                    keepalive 2000;}server {    listen       80;                                                             server_name  10.0.0.1;                                                   client_max_body_size 1024M;    location /my/ {        proxy_pass http://my_server;        proxy_set_header Host $host:$server_port;    }}

Then, the request to access the nginx address http: // 10.0.0.1: 80/my will be forwarded to the my_server service address http: // 10.0.0.2: 8080/my. This is because if the proxy_pass parameter does not contain the url path, the path identified by the location pattern will be used as the absolute path.

2. Redirect Message proxy

Even if the nginx proxy is configured, when the service returns a redirected message (http code 301 or 302), the redirected destination url is placed in the location field of the header of the http response Message. When the browser receives the redirection message, it parses the field and performs the jump. At this time, the new request message will be sent directly to the service address instead of the nginx address. To allow nginx to intercept such requests, you must modify the location information of the redirection message.

location /my/ {    proxy_pass http://my_server;    proxy_set_header Host $host:$server_port;    proxy_redirect / /my/;}

You can use proxy_redirect to modify the location field of the redirection message. In this example, all URLs under the root path are forwarded to the/my/path of nginx and returned to the user. For example, if the original location Value of the redirection packet returned by the service is/login, the location field of the packet received by the user is/my/login after the nginx proxy. In this case, the browser will jump to the/my/login address of nginx for access.

Note that the location field of the redirection packet returned by the service sometimes fills in the absolute path (including the ip address, domain name, and port of the service), and sometimes fills in the relative path, in this case, you need to identify based on the actual situation.

location /my/ {    proxy_pass http://my_server;    proxy_set_header Host $host:$server_port;    proxy_redirect http://my_server/ http://$host:$server_port/my/;}

The above configuration is to proxy all paths under the root path of the my_server service to the/my/path of the nginx address. When nginx is configured with only one server,http://$host:$server_portThe prefix can be omitted.

3. Message data replacement

When the nginx proxy is used as the best (dan) Force (sui), the service address or absolute web path is written to the http response packet. It is rare to write a dead service address, but it also occasionally exists. The most tricky thing is to write the absolute web path, especially the absolute path does not have a public prefix. For example:

Generally, a web page contains the following similar paths:

  • /Public: used for Static Page resources, such as js scripts/public/js, style sheets/public/css, images/public/img, etc.
  • /Static: similar to/public.
  • /Api: used for backend Service API interfaces.
  • /Login: used for Logon verification.
  • Others.

For such a service, the possible proxy configuration is as follows:

location /my/ {    proxy_pass http://my_server/;    proxy_set_header Host $host:$server_port;    proxy_redirect / /my/;}location /login/ {    proxy_pass http://my_server/public;    proxy_set_header Host $host:$server_port;}location /public/ {    proxy_pass http://my_server/public;    proxy_set_header Host $host:$server_port;}location /api/ {    proxy_pass http://my_server/api;    proxy_set_header Host $host:$server_port;}

Because similar absolute paths are written to web pages or static resources, when users jump through links in the page, they will request to the corresponding path of the nginx service. Once another service also contains a similar path and nginx proxy is required, the conflict arises: Which service does the request to access the same path of nginx be forwarded?

To solve this problem, you must add a uniform prefix to the absolute path contained in the packet data before receiving the message, such as/my/public,/my/api, /my/login, so that the nginx proxy configuration can be simplified:

location /my/ {    proxy_pass http://my_server/;    proxy_set_header Host $host:$server_port;    proxy_redirect / /my/;}location /other/ {    proxy_pass http://other_server/;    proxy_set_header Host $host:$server_port;    proxy_redirect / /other/;}

Nginx ngx_http_sub_module module provides similar packet data replacement function, this module is not installed by default, you need to add -- with-http_sub_module parameters when compiling nginx, or directly download nginx rpm package.

The syntax for replacing data packets with sub_filter is as follows:

location /my/ {    proxy_pass http://my_server/;    proxy_set_header Host $host:$server_port;        sub_filter 'href="/' 'href="/my/';    sub_filter 'src="/' 'src="/my/';    sub_filter_types text/html;    sub_filter_once  off;}

The above configuration will replace href = "/with href ="/my "for all response packets under/my/, and src ="/with src = "/my, add a public prefix to all absolute paths.

Note: To configure multiple sub_filters, ensure that nginx is later than version 1.9.4.

4. Summary

Even so, sub_filter cannot solve all the problems. Currently, popular js frameworks have the ability to automatically render URLs. That is to say, many absolute paths are not written to static pages, but are also dynamically generated by the js Code framework. In this situation, sub_filter is also powerless. For such a situation, I can only sincerely advise, or quietly change the code!

For more Nginx tutorials, see the following:

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.