Operations 4 min read

Using Nginx Rewrite and Proxy_pass for URL Redirection (Changing and Preserving Browser URL)

This article explains how to perform URL redirection with Nginx by using the rewrite module for changing the browser address and the location/proxy_pass combination for keeping the address unchanged, including flag options and two practical configuration examples.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Using Nginx Rewrite and Proxy_pass for URL Redirection (Changing and Preserving Browser URL)

In daily operations you often need to redirect from one link to another, which can be divided into two scenarios: the browser URL changes or the browser URL remains unchanged.

1. URL changes – use the Nginx rewrite module. The rewrite directive rewrites the request URI based on a regular expression and redirects to a replacement, ending with a flag.

The flag can be one of the following:

last – after the rule matches, continue processing with the new location URI.

break – stop processing any further rules.

redirect – return a 302 temporary redirect; the browser shows the new URL.

permanent – return a 301 permanent redirect; the browser shows the new URL.

Typically a 301 permanent redirect is used to implement URL redirection.

2. URL unchanged – use location with proxy_pass or rewrite with last or break . The location block’s proxy_pass can forward the request while keeping the original address visible to the user.

Example (1): Redirect https://api.gwhome.com/data/app to https://images.com/data/app/gw.html .

server {
    listen      443;
    server_name  gwhome;
    access_log  /data/nginx/logs/gwhome-access.log main;
    error_log   /data/nginx/logs/gwhome-error.log;
    ssl on;
    ssl_certificate /data/nginx/ssl/gwhome.crt;
    ssl_certificate_key /data/nginx/ssl/gwhome.key;
    ssl_session_timeout 5m;

    location = /data/app {
        rewrite /data/app /data/app/gw.html break;
        proxy_pass https://images.com;
    }
}

Example (2): Redirect requests from 192.168.210.85:8190/gwgou/order/commdany to 192.164.60.89:8089/order/commdany .

server {
    listen 8190;
    server_name 192.168.210.85;
    index   index.html index.php index.htm;
    location ~* ^/gwgou/order/commdany {
        proxy_next_upstream error timeout http_503 http_504 http_502;
        proxy_connect_timeout 500s;
        proxy_read_timeout 500s;
        proxy_send_timeout 500s;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        rewrite  ^(.*)$  /order/commdany break;
        proxy_pass http://192.164.60.89:8089;
    }
}

For further reading, see related articles on Nginx access control, Kafka cluster deployment, Redis Sentinel setup, MySQL binlog recovery, and building a high‑availability Kubernetes cluster.

operationsNginxurl-rewriteRedirectproxy_pass
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.