Serving django app in an intended location (NGINX)












1















I have a web server serving my Django app (using NGINX) and I need to access it in a defined "location".



For example, I access my Django app XPTO in "ip:port/" but I need to access it like "ip:port/XPTO/". All urls specified in Django have to be resolved "after" this "base url".



Anyway I can do this without messing with my "urls.py" in Django? I had tried some configurations on NGINX but nothing worked.



Thanks in advance!










share|improve this question



























    1















    I have a web server serving my Django app (using NGINX) and I need to access it in a defined "location".



    For example, I access my Django app XPTO in "ip:port/" but I need to access it like "ip:port/XPTO/". All urls specified in Django have to be resolved "after" this "base url".



    Anyway I can do this without messing with my "urls.py" in Django? I had tried some configurations on NGINX but nothing worked.



    Thanks in advance!










    share|improve this question

























      1












      1








      1








      I have a web server serving my Django app (using NGINX) and I need to access it in a defined "location".



      For example, I access my Django app XPTO in "ip:port/" but I need to access it like "ip:port/XPTO/". All urls specified in Django have to be resolved "after" this "base url".



      Anyway I can do this without messing with my "urls.py" in Django? I had tried some configurations on NGINX but nothing worked.



      Thanks in advance!










      share|improve this question














      I have a web server serving my Django app (using NGINX) and I need to access it in a defined "location".



      For example, I access my Django app XPTO in "ip:port/" but I need to access it like "ip:port/XPTO/". All urls specified in Django have to be resolved "after" this "base url".



      Anyway I can do this without messing with my "urls.py" in Django? I had tried some configurations on NGINX but nothing worked.



      Thanks in advance!







      python django web nginx webserver






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 14:39









      José Pedro DuarteJosé Pedro Duarte

      368




      368
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You need to use the location directive in your nginx config.



          You probably have something right now that looks like this:



          location / {
          ...
          }


          To serve under XPTO instead you want this:



          location /XPTO/ {
          ...
          }


          You also need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.



          If this doesn't work, please show us your current nginx config (edit it into the question) and we may be able to provide more specific advice.






          share|improve this answer


























          • I had tried that before also and it seems to be the way but it does not "fully" work as expected. It works at the NGINX level (i.e. NGINX accepts the correct URLS and denies the others) but then Django removes the /XPTO/ in the url and then NGINX does not know the next location. I want Django to assume it always as the base URL so that the /XPTO/ is always on the URL. If I was too confusing, what happens is: /xpto/users -> NGINX, /users/ -> Django, /users/2/ -> NGINX, NGINX does not know that location.

            – José Pedro Duarte
            Nov 13 '18 at 15:09








          • 1





            Django does not remove /XPTO/ from url. When you ask NGINX for /XPTO/ uri it will return 302 status code and redirect to your UWSGI root url, which is your django root url

            – Ilko
            Nov 13 '18 at 15:37






          • 1





            You need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.

            – Daniel Roseman
            Nov 13 '18 at 17:40











          • @DanielRoseman great, thanks, I've added that information to the answer.

            – Rob Bricheno
            Nov 13 '18 at 17:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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%2fstackoverflow.com%2fquestions%2f53283421%2fserving-django-app-in-an-intended-location-nginx%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You need to use the location directive in your nginx config.



          You probably have something right now that looks like this:



          location / {
          ...
          }


          To serve under XPTO instead you want this:



          location /XPTO/ {
          ...
          }


          You also need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.



          If this doesn't work, please show us your current nginx config (edit it into the question) and we may be able to provide more specific advice.






          share|improve this answer


























          • I had tried that before also and it seems to be the way but it does not "fully" work as expected. It works at the NGINX level (i.e. NGINX accepts the correct URLS and denies the others) but then Django removes the /XPTO/ in the url and then NGINX does not know the next location. I want Django to assume it always as the base URL so that the /XPTO/ is always on the URL. If I was too confusing, what happens is: /xpto/users -> NGINX, /users/ -> Django, /users/2/ -> NGINX, NGINX does not know that location.

            – José Pedro Duarte
            Nov 13 '18 at 15:09








          • 1





            Django does not remove /XPTO/ from url. When you ask NGINX for /XPTO/ uri it will return 302 status code and redirect to your UWSGI root url, which is your django root url

            – Ilko
            Nov 13 '18 at 15:37






          • 1





            You need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.

            – Daniel Roseman
            Nov 13 '18 at 17:40











          • @DanielRoseman great, thanks, I've added that information to the answer.

            – Rob Bricheno
            Nov 13 '18 at 17:54
















          1














          You need to use the location directive in your nginx config.



          You probably have something right now that looks like this:



          location / {
          ...
          }


          To serve under XPTO instead you want this:



          location /XPTO/ {
          ...
          }


          You also need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.



          If this doesn't work, please show us your current nginx config (edit it into the question) and we may be able to provide more specific advice.






          share|improve this answer


























          • I had tried that before also and it seems to be the way but it does not "fully" work as expected. It works at the NGINX level (i.e. NGINX accepts the correct URLS and denies the others) but then Django removes the /XPTO/ in the url and then NGINX does not know the next location. I want Django to assume it always as the base URL so that the /XPTO/ is always on the URL. If I was too confusing, what happens is: /xpto/users -> NGINX, /users/ -> Django, /users/2/ -> NGINX, NGINX does not know that location.

            – José Pedro Duarte
            Nov 13 '18 at 15:09








          • 1





            Django does not remove /XPTO/ from url. When you ask NGINX for /XPTO/ uri it will return 302 status code and redirect to your UWSGI root url, which is your django root url

            – Ilko
            Nov 13 '18 at 15:37






          • 1





            You need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.

            – Daniel Roseman
            Nov 13 '18 at 17:40











          • @DanielRoseman great, thanks, I've added that information to the answer.

            – Rob Bricheno
            Nov 13 '18 at 17:54














          1












          1








          1







          You need to use the location directive in your nginx config.



          You probably have something right now that looks like this:



          location / {
          ...
          }


          To serve under XPTO instead you want this:



          location /XPTO/ {
          ...
          }


          You also need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.



          If this doesn't work, please show us your current nginx config (edit it into the question) and we may be able to provide more specific advice.






          share|improve this answer















          You need to use the location directive in your nginx config.



          You probably have something right now that looks like this:



          location / {
          ...
          }


          To serve under XPTO instead you want this:



          location /XPTO/ {
          ...
          }


          You also need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.



          If this doesn't work, please show us your current nginx config (edit it into the question) and we may be able to provide more specific advice.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 17:53

























          answered Nov 13 '18 at 14:47









          Rob BrichenoRob Bricheno

          2,335318




          2,335318













          • I had tried that before also and it seems to be the way but it does not "fully" work as expected. It works at the NGINX level (i.e. NGINX accepts the correct URLS and denies the others) but then Django removes the /XPTO/ in the url and then NGINX does not know the next location. I want Django to assume it always as the base URL so that the /XPTO/ is always on the URL. If I was too confusing, what happens is: /xpto/users -> NGINX, /users/ -> Django, /users/2/ -> NGINX, NGINX does not know that location.

            – José Pedro Duarte
            Nov 13 '18 at 15:09








          • 1





            Django does not remove /XPTO/ from url. When you ask NGINX for /XPTO/ uri it will return 302 status code and redirect to your UWSGI root url, which is your django root url

            – Ilko
            Nov 13 '18 at 15:37






          • 1





            You need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.

            – Daniel Roseman
            Nov 13 '18 at 17:40











          • @DanielRoseman great, thanks, I've added that information to the answer.

            – Rob Bricheno
            Nov 13 '18 at 17:54



















          • I had tried that before also and it seems to be the way but it does not "fully" work as expected. It works at the NGINX level (i.e. NGINX accepts the correct URLS and denies the others) but then Django removes the /XPTO/ in the url and then NGINX does not know the next location. I want Django to assume it always as the base URL so that the /XPTO/ is always on the URL. If I was too confusing, what happens is: /xpto/users -> NGINX, /users/ -> Django, /users/2/ -> NGINX, NGINX does not know that location.

            – José Pedro Duarte
            Nov 13 '18 at 15:09








          • 1





            Django does not remove /XPTO/ from url. When you ask NGINX for /XPTO/ uri it will return 302 status code and redirect to your UWSGI root url, which is your django root url

            – Ilko
            Nov 13 '18 at 15:37






          • 1





            You need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.

            – Daniel Roseman
            Nov 13 '18 at 17:40











          • @DanielRoseman great, thanks, I've added that information to the answer.

            – Rob Bricheno
            Nov 13 '18 at 17:54

















          I had tried that before also and it seems to be the way but it does not "fully" work as expected. It works at the NGINX level (i.e. NGINX accepts the correct URLS and denies the others) but then Django removes the /XPTO/ in the url and then NGINX does not know the next location. I want Django to assume it always as the base URL so that the /XPTO/ is always on the URL. If I was too confusing, what happens is: /xpto/users -> NGINX, /users/ -> Django, /users/2/ -> NGINX, NGINX does not know that location.

          – José Pedro Duarte
          Nov 13 '18 at 15:09







          I had tried that before also and it seems to be the way but it does not "fully" work as expected. It works at the NGINX level (i.e. NGINX accepts the correct URLS and denies the others) but then Django removes the /XPTO/ in the url and then NGINX does not know the next location. I want Django to assume it always as the base URL so that the /XPTO/ is always on the URL. If I was too confusing, what happens is: /xpto/users -> NGINX, /users/ -> Django, /users/2/ -> NGINX, NGINX does not know that location.

          – José Pedro Duarte
          Nov 13 '18 at 15:09






          1




          1





          Django does not remove /XPTO/ from url. When you ask NGINX for /XPTO/ uri it will return 302 status code and redirect to your UWSGI root url, which is your django root url

          – Ilko
          Nov 13 '18 at 15:37





          Django does not remove /XPTO/ from url. When you ask NGINX for /XPTO/ uri it will return 302 status code and redirect to your UWSGI root url, which is your django root url

          – Ilko
          Nov 13 '18 at 15:37




          1




          1





          You need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.

          – Daniel Roseman
          Nov 13 '18 at 17:40





          You need to be sure that you generate all internal links via the url tag or the reverse function, so that they will automatically include the prefix.

          – Daniel Roseman
          Nov 13 '18 at 17:40













          @DanielRoseman great, thanks, I've added that information to the answer.

          – Rob Bricheno
          Nov 13 '18 at 17:54





          @DanielRoseman great, thanks, I've added that information to the answer.

          – Rob Bricheno
          Nov 13 '18 at 17:54


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53283421%2fserving-django-app-in-an-intended-location-nginx%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

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues