Nginx: map subdomain to a subdirectory on proxied serverHelp needed setting up nginx to serve static...

Have I saved too much for retirement so far?

My boss asked me to take a one-day class, then signs it up as a day off

Greatest common substring

Can the harmonic series explain the origin of the major scale?

Simple recursive Sudoku solver

Can a Bard use an arcane focus?

What is the opposite of 'gravitas'?

Lightning Web Component - do I need to track changes for every single input field in a form

Can I Retrieve Email Addresses from BCC?

What does the "3am" section means in manpages?

Books on the History of math research at European universities

Simple image editor tool to draw a simple box/rectangle in an existing image

For airliners, what prevents wing strikes on landing in bad weather?

Identify a stage play about a VR experience in which participants are encouraged to simulate performing horrific activities

Giant Toughroad SLR 2 for 200 miles in two days, will it make it?

Could solar power be utilized and substitute coal in the 19th century?

Science Fiction story where a man invents a machine that can help him watch history unfold

Is there enough fresh water in the world to eradicate the drinking water crisis?

Can the electrostatic force be infinite in magnitude?

Is it okay / does it make sense for another player to join a running game of Munchkin?

Pronouncing Homer as in modern Greek

Does "Dominei" mean something?

Can a malicious addon access internet history and such in chrome/firefox?

Meta programming: Declare a new struct on the fly



Nginx: map subdomain to a subdirectory on proxied server


Help needed setting up nginx to serve static filesBlank Page: wordpress on nginx+php-fpmnginx redirect issue with upstream configurationNginx proxy pass works for https but not httpnginx proxy redirecting request to different proxyNginx subversion commit failurenginx reverse proxy hide login query also on 301 redirect or full qualified urlCodeIgniter nginx rewrite rules for i8ln URL'sMap subdomain to subdirectory and custom header to basic authenticationNginx reverse proxy to many local servers + webserver duty













0















I have domain example.com and subdomain blog.example.com. I have an Unicorn application running at localhost:5000, and use Nginx as a reverse proxy.



I had no issues when running just the example.com. However I want to add subdomain support and have some issues.



I have some content at example.com/blog. I want blog.example.com to point to it, without user knowledge that a rewrite is used. I want to map all URLS, so that:





  • blog.example.com -> localhost:5000/blog


  • blog.example.com/index.php -> localhost:5000/blog/index.php


  • blog.example.com/foo/bar -> localhost:5000/blog/foo/bar


My best attempt so far it this:



server
{
listen 80;
server_name blog.example.com;

location / {
proxy_pass http://localhost:5000/blog/$uri;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}

client_max_body_size 4G;
keepalive_timeout 10;
}


This correctly rewrites blog.example.com, but fails with blog.example.com/index.php:



$ curl -v 'http://blog.example.com'
> GET /index.php HTTP/1.1
> Host: blog.example.com
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Sun, 26 Mar 2017 12:29:00 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 337
< Connection: keep-alive
< Location: http://blog.example.com/blog/index.php









share|improve this question























  • I think this is something in your blog software configuration that makes it return the redirect.

    – Tero Kilkanen
    Mar 26 '17 at 14:28











  • No, this is on nginx level. I set up a script at /blog that does nothing except printing env info (like php_info()) and I'm still getting the 301 from nginx. Also, when it starts to redirect to /blog, it creates a loop (/blog/blog/blog/blog/.../blog/index.php).

    – Michael
    Mar 26 '17 at 14:33


















0















I have domain example.com and subdomain blog.example.com. I have an Unicorn application running at localhost:5000, and use Nginx as a reverse proxy.



I had no issues when running just the example.com. However I want to add subdomain support and have some issues.



I have some content at example.com/blog. I want blog.example.com to point to it, without user knowledge that a rewrite is used. I want to map all URLS, so that:





  • blog.example.com -> localhost:5000/blog


  • blog.example.com/index.php -> localhost:5000/blog/index.php


  • blog.example.com/foo/bar -> localhost:5000/blog/foo/bar


My best attempt so far it this:



server
{
listen 80;
server_name blog.example.com;

location / {
proxy_pass http://localhost:5000/blog/$uri;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}

client_max_body_size 4G;
keepalive_timeout 10;
}


This correctly rewrites blog.example.com, but fails with blog.example.com/index.php:



$ curl -v 'http://blog.example.com'
> GET /index.php HTTP/1.1
> Host: blog.example.com
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Sun, 26 Mar 2017 12:29:00 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 337
< Connection: keep-alive
< Location: http://blog.example.com/blog/index.php









share|improve this question























  • I think this is something in your blog software configuration that makes it return the redirect.

    – Tero Kilkanen
    Mar 26 '17 at 14:28











  • No, this is on nginx level. I set up a script at /blog that does nothing except printing env info (like php_info()) and I'm still getting the 301 from nginx. Also, when it starts to redirect to /blog, it creates a loop (/blog/blog/blog/blog/.../blog/index.php).

    – Michael
    Mar 26 '17 at 14:33
















0












0








0








I have domain example.com and subdomain blog.example.com. I have an Unicorn application running at localhost:5000, and use Nginx as a reverse proxy.



I had no issues when running just the example.com. However I want to add subdomain support and have some issues.



I have some content at example.com/blog. I want blog.example.com to point to it, without user knowledge that a rewrite is used. I want to map all URLS, so that:





  • blog.example.com -> localhost:5000/blog


  • blog.example.com/index.php -> localhost:5000/blog/index.php


  • blog.example.com/foo/bar -> localhost:5000/blog/foo/bar


My best attempt so far it this:



server
{
listen 80;
server_name blog.example.com;

location / {
proxy_pass http://localhost:5000/blog/$uri;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}

client_max_body_size 4G;
keepalive_timeout 10;
}


This correctly rewrites blog.example.com, but fails with blog.example.com/index.php:



$ curl -v 'http://blog.example.com'
> GET /index.php HTTP/1.1
> Host: blog.example.com
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Sun, 26 Mar 2017 12:29:00 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 337
< Connection: keep-alive
< Location: http://blog.example.com/blog/index.php









share|improve this question














I have domain example.com and subdomain blog.example.com. I have an Unicorn application running at localhost:5000, and use Nginx as a reverse proxy.



I had no issues when running just the example.com. However I want to add subdomain support and have some issues.



I have some content at example.com/blog. I want blog.example.com to point to it, without user knowledge that a rewrite is used. I want to map all URLS, so that:





  • blog.example.com -> localhost:5000/blog


  • blog.example.com/index.php -> localhost:5000/blog/index.php


  • blog.example.com/foo/bar -> localhost:5000/blog/foo/bar


My best attempt so far it this:



server
{
listen 80;
server_name blog.example.com;

location / {
proxy_pass http://localhost:5000/blog/$uri;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}

client_max_body_size 4G;
keepalive_timeout 10;
}


This correctly rewrites blog.example.com, but fails with blog.example.com/index.php:



$ curl -v 'http://blog.example.com'
> GET /index.php HTTP/1.1
> Host: blog.example.com
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Sun, 26 Mar 2017 12:29:00 GMT
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 337
< Connection: keep-alive
< Location: http://blog.example.com/blog/index.php






nginx proxy rewrite subdomain






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 '17 at 12:39









MichaelMichael

113




113













  • I think this is something in your blog software configuration that makes it return the redirect.

    – Tero Kilkanen
    Mar 26 '17 at 14:28











  • No, this is on nginx level. I set up a script at /blog that does nothing except printing env info (like php_info()) and I'm still getting the 301 from nginx. Also, when it starts to redirect to /blog, it creates a loop (/blog/blog/blog/blog/.../blog/index.php).

    – Michael
    Mar 26 '17 at 14:33





















  • I think this is something in your blog software configuration that makes it return the redirect.

    – Tero Kilkanen
    Mar 26 '17 at 14:28











  • No, this is on nginx level. I set up a script at /blog that does nothing except printing env info (like php_info()) and I'm still getting the 301 from nginx. Also, when it starts to redirect to /blog, it creates a loop (/blog/blog/blog/blog/.../blog/index.php).

    – Michael
    Mar 26 '17 at 14:33



















I think this is something in your blog software configuration that makes it return the redirect.

– Tero Kilkanen
Mar 26 '17 at 14:28





I think this is something in your blog software configuration that makes it return the redirect.

– Tero Kilkanen
Mar 26 '17 at 14:28













No, this is on nginx level. I set up a script at /blog that does nothing except printing env info (like php_info()) and I'm still getting the 301 from nginx. Also, when it starts to redirect to /blog, it creates a loop (/blog/blog/blog/blog/.../blog/index.php).

– Michael
Mar 26 '17 at 14:33







No, this is on nginx level. I set up a script at /blog that does nothing except printing env info (like php_info()) and I'm still getting the 301 from nginx. Also, when it starts to redirect to /blog, it creates a loop (/blog/blog/blog/blog/.../blog/index.php).

– Michael
Mar 26 '17 at 14:33












2 Answers
2






active

oldest

votes


















1














I found that my mistake was at this line:



proxy_pass http://localhost:5000/blog/$uri;


When changed to



proxy_pass http://localhost:5000/blog$uri;


the proxy works as expected.



[edit]



Even better version, to also pass query string:



proxy_pass http://localhost:5000/blog$uri$is_args$args;





share|improve this answer

































    0














    Do you have a problem with styles and javascripts?
    The assets in my case throw an error (404)





    share








    New contributor




    drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "2"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f840654%2fnginx-map-subdomain-to-a-subdirectory-on-proxied-server%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      I found that my mistake was at this line:



      proxy_pass http://localhost:5000/blog/$uri;


      When changed to



      proxy_pass http://localhost:5000/blog$uri;


      the proxy works as expected.



      [edit]



      Even better version, to also pass query string:



      proxy_pass http://localhost:5000/blog$uri$is_args$args;





      share|improve this answer






























        1














        I found that my mistake was at this line:



        proxy_pass http://localhost:5000/blog/$uri;


        When changed to



        proxy_pass http://localhost:5000/blog$uri;


        the proxy works as expected.



        [edit]



        Even better version, to also pass query string:



        proxy_pass http://localhost:5000/blog$uri$is_args$args;





        share|improve this answer




























          1












          1








          1







          I found that my mistake was at this line:



          proxy_pass http://localhost:5000/blog/$uri;


          When changed to



          proxy_pass http://localhost:5000/blog$uri;


          the proxy works as expected.



          [edit]



          Even better version, to also pass query string:



          proxy_pass http://localhost:5000/blog$uri$is_args$args;





          share|improve this answer















          I found that my mistake was at this line:



          proxy_pass http://localhost:5000/blog/$uri;


          When changed to



          proxy_pass http://localhost:5000/blog$uri;


          the proxy works as expected.



          [edit]



          Even better version, to also pass query string:



          proxy_pass http://localhost:5000/blog$uri$is_args$args;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 26 '17 at 15:00

























          answered Mar 26 '17 at 14:42









          MichaelMichael

          113




          113

























              0














              Do you have a problem with styles and javascripts?
              The assets in my case throw an error (404)





              share








              New contributor




              drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                0














                Do you have a problem with styles and javascripts?
                The assets in my case throw an error (404)





                share








                New contributor




                drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  0












                  0








                  0







                  Do you have a problem with styles and javascripts?
                  The assets in my case throw an error (404)





                  share








                  New contributor




                  drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  Do you have a problem with styles and javascripts?
                  The assets in my case throw an error (404)






                  share








                  New contributor




                  drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share


                  share






                  New contributor




                  drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 1 min ago









                  drmartindrmartin

                  1011




                  1011




                  New contributor




                  drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  drmartin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Server Fault!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f840654%2fnginx-map-subdomain-to-a-subdirectory-on-proxied-server%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      As a Security Precaution, the user account has been locked The Next CEO of Stack OverflowMS...

                      Українські прізвища Зміст Історичні відомості |...

                      Список ссавців Італії Природоохоронні статуси | Список |...