Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:57:21+00:00 2026-06-18T01:57:21+00:00

The nginx documentation says that the server_name directive supports regular expressions. I’ve been banging

  • 0

The nginx documentation says that the server_name directive supports regular expressions. I’ve been banging my head against the wall trying to get even a trivial regex working.

I want http://subdomain.mydomain.com to redirect to http://mydomain.com/subdomain

Here is my code.

server {
  server_name "~^subdomain\.mydomain\.com$";
  rewrite ^ http://mydomain.com/subdomain;
}

Also, potentially noteworthy. Further down in the nginx config file there is a rule:

server {
  server_name *.mydomain.com
  ...
}

What am I doing wrong?

UPDATE:

It has been suggested that I not use regex for this… to offer a little more clarity: the trivial regex was simply for purposes of troubleshooting. The real regex will look more like…

server {
  server_name "~^.*(cvg|cincinnati)\.fakeairport(app)?\.(org|com)$";
  rewrite ^ http://fakeairport.com/cincinnati;
}

server {
  server_name "~^.*(lex|lexington)\.fakeairport(app)?\.(org|com)$";
  rewrite ^ http://fakeairport.com/lexington;
}

So it would be preferable to use regex.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:57:22+00:00Added an answer on June 18, 2026 at 1:57 am

    To answer an old question to help others

    using nginx 1.1.19 you can do the following:

    server {
        server_name     ~^(?<subdomain>\w+)\.domainA\.com$;
    
        location / {
                rewrite ^ https://$subdomain.domainB.com$request_uri permanent;
        }
    }
    

    The subdomain before domainA.com is matched and stored in variable $subdomain which then can be used in the rewrite.
    This rewrites url like xxx.domainA.com to xxx.domainB.com with only one server directive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When looking at the documentation for nginx's error_page directive, it seems that one has
Nginx 1.1.4+ can serve upstream connection with HTTP1.1 keepalive directive, see the official documentation
I see the Nginx HttpRewriteModule documentation has an example to rewrite a www-prefixed domain
When configuring nginx with a site that has ssl, the examples I find online
In looking at the documentation , it appears that DropWizard is only able to
Does NGINX or Apache benefit from a server that has either: Multi-cores, or Multiple
I'm using Nginx to reverse proxy my Unicorn process for a Rails app that
nginx seems to be replacing the Connection: close header that upstream is sending, and
My nginx settings are like this : server { listen 80; server_name websitenamne.in www.websitename.in;
According to the Nginx documentation : If you need to replace nginx binary with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.