'Nginx bulk URL redirect by include a separated file but not working as I want

I am not very familiar with Nginx URL redirect. But I have followed some suggestions from Google search and unfortunately none of them meet my expectation.

I need to redirect at least 100 old URLs to different new URLs. I already have another server.conf file under /etc/nginx/conf.d/ for some URL redirect and other settings. But I would like to create another nginx_redirect.txt file to store those 100 URLs redirect and keep away from server.conf. Furthermore, some of the old 100 URLs are also defined in server.conf file, but I was hoping the new nginx_rewrite.txt file can override the redirect URLs defined in server.conf file.

I use multiple "include" in the nginx.conf as below:

include /etc/nginx/conf.d/*.conf;
Server {
   ...
   include /etc/nginx/conf.d/nginx_rewrite.txt;
   ...
}

The existing server.conf file for some URL redirect as below:

server {
    ...
    location ~ ^/lp/old/ {
        rewrite ^(.*)$ https://currenturl.com/ permanent;
    }
}

I create new nginx_redirect.txt and hope to override the redirect URL in server.conf.

location ~ ^/lp/old/ {
    rewrite ^(.*)$ https://newurl.com/ permanent;
}

I used below commands to ensure those .conf and .txt can be seen by Nginx.

nginx -T
nginx -s reload

However I found those old URLs defined in nginx_rewrite.txt didn't redirect to newurl.com/ but still to currenturl.com/. Even I set up some new URL in nginx_rewrite.txt, which is not defined in server.conf. Those new URL will show 404 instead of redirecting to other URL. Unless I defined those new URL in server.conf.

My questions:

  1. Is my setting in nginx.conf, server.conf and nginx_rewrite.txt correct?
  2. Is there not possible to override URL in permanent redirect of .conf?

Updated on 5/10 to share the result of nginx -T. Sorry I have remove some lines since it is not suitable to share with public.

sh-4.2$ sudo nginx -T
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# configuration file /etc/nginx/nginx.conf:
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        include /etc/nginx/conf.d/nginx_rewrite.txt;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

# configuration file /etc/nginx/conf.d/server.conf:
upstream nginx-internal-sock {
    server unix:/var/www/server/shared/tmp/unicorn.sock;
}

server {
    listen 80;
    server_name server.com;

    location / {
        root /var/www/server/current/public/;
    }
}

server {
    listen 80;
    server_name stg.server.com;

    real_ip_header  X-Forwarded-For;

    location / {
        proxy_set_header Accept-Encoding "";
        proxy_pass http://nginx-internal-sock/sales/;
    }

    location /sales {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Accept-Encoding "";
        proxy_pass http://nginx-internal-sock/sales;
    }

     location ~ ^/(assets)/ {
        root /var/www/server/current/public;
        add_header Cache-Control no-cache;
        expires 7d;
        # add_header Last-Modified "";
        # add_header ETag "";
    }

    location ~ ^/lp/old/ {
        rewrite ^(.*)$ https://currenturl.com/ permanent;
    }
    ...
   
}

# configuration file /etc/nginx/conf.d/nginx_rewrite.txt:
location ~ ^/lp/old/ {
        rewrite ^(.*)$ https://newurl.com/ permanent;
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source