Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy to subdirectory #15464

Open
ndc opened this issue Nov 2, 2019 — with docs.microsoft.com · 12 comments
Open

Deploy to subdirectory #15464

ndc opened this issue Nov 2, 2019 — with docs.microsoft.com · 12 comments

Comments

Copy link

@ndc ndc commented Nov 2, 2019 — with docs.microsoft.com

I need to host multiple apps in the same server under different subdirectories, for example http://myserver/myfirstapp and http://myserver/mysecondapp.
I tried to set up nginx with multiple locations, but request from browser can't seem to reach the app (404). It seems like the app works only if put on root (location /).
Need guidance on how to set up multiple apps in subdirectories with nginx.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@guardrex

This comment has been minimized.

Copy link
Collaborator

@guardrex guardrex commented Nov 2, 2019

Hello @ndc ... this is akin to (not really an exact duplicate given it's a different server) #14999. See if the SO answer that was posted there is any help (it may only apply to Apache).

I'll mark this for the Product Unit ("PU" label). No ETA on when they'll get to this. In the meantime, your best bet are support forums and chat. See my answer on that other issue ☝️.

@guardrex guardrex added this to the Backlog milestone Nov 2, 2019
@mkArtakMSFT mkArtakMSFT added the P3 label Dec 6, 2019
@mkArtakMSFT

This comment has been minimized.

Copy link
Contributor

@mkArtakMSFT mkArtakMSFT commented Dec 6, 2019

@sheng-jie

This comment has been minimized.

Copy link

@sheng-jie sheng-jie commented Feb 22, 2020

@ndc Just like this;

  1. Nginx config:
location /myfirstapp/ {
        proxy_pass http://ip1:port1/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }


    location /mysecondapp/ {
        proxy_pass http://ip2:port2/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    location / {
        proxy_pass http://ip:port/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

Don't forget the last trailing / under proxy_pass !

  1. Add app.UsePathBase("/myfirstapp") in your first project andapp.UsePathBase("/mysecondapp") in your second project.
@ndc

This comment has been minimized.

Copy link
Author

@ndc ndc commented Feb 23, 2020

@sheng-jie Thanks! It turned out only UsePathBase is needed to make it work. I created a blog post to gather my notes when working on this.

@guardrex

This comment has been minimized.

Copy link
Collaborator

@guardrex guardrex commented Feb 23, 2020

Thanks for the work you guys have done on this issue. I'll schedule it and see if I can improve the doc based on your findings. I should be able to squeeze it in between rounds of Blazor doc issues on the next sprint (March).

@guardrex guardrex self-assigned this Feb 23, 2020
@guardrex guardrex removed the PU label Feb 23, 2020
@guardrex guardrex added this to To do in Next sprint via automation Feb 23, 2020
@guardrex guardrex modified the milestones: Backlog, 2020 Q1 ends Mar 31 Feb 23, 2020
@sheng-jie

This comment has been minimized.

Copy link

@sheng-jie sheng-jie commented Feb 24, 2020

@sheng-jie Thanks! It turned out only UsePathBase is needed to make it work. I created a blog post to gather my notes when working on this.

@ndc , You're welcome. You summarized it very carefully. I’m wondering if I can translate your article to Chinese and post on my blog.

@ndc

This comment has been minimized.

Copy link
Author

@ndc ndc commented Feb 24, 2020

@sheng-jie no problem, go ahead!

@sheng-jie

This comment has been minimized.

Copy link

@sheng-jie sheng-jie commented Feb 26, 2020

I found a new issue when configuring proxy_path as below.

location /myfirstapp/ {
        proxy_pass http://ip1:port1/;

For example there is a tag <a asp-controller="Home" asp-action="About">About</a> in razor page. It will generate a tag like that: <a href="/Home/About">About</a>. In this way, we will route to wrong endpoint.

To solve this issue we can just add subpath to proxy_pass like below and you will get right url path <a href="/myfirstapp/Home/About">About</a>.

location /myfirstapp/ {
        proxy_pass http://ip1:port1/myfirstapp/;

This comment has been minimized.

Copy link

@mu88 mu88 commented Mar 16, 2020 — with docs.microsoft.com

Can someone please explain to me why we have to set UseBasePath when using Nginx? On IIS, I can simply create a reverse proxy from http://localhost/demo to http://localhost:8000 and it works without setting UseBasePath. It seems strange to me that I need to add a deployment feature to my code - I'd expect the code to be "deployment agnostic".

@guardrex

This comment has been minimized.

Copy link
Collaborator

@guardrex guardrex commented Mar 16, 2020

@mu88 The app doesn't know where it's deployed, and the app has to be able to create links to resources, for example. Such links won't be correct if it doesn't know its base path.

For IIS/ANCM ...

Middleware added by IIS Integration updates the scheme, remote IP, and pathbase to account for forwarding the request to Kestrel.

For out-of-process, CreateDefaultBuilder calls UseIISIntegration to ... Configure the port and base path the server should listen on when running behind the ASP.NET Core Module.

That's how the app knows in that scenario ... the ANCM/middleware takes care of it.

@mu88

This comment has been minimized.

Copy link

@mu88 mu88 commented Mar 16, 2020

@guardrex Thank you for your explanation. Do you know the reason with UseBasePath is baken into the code, rather than specifying a configuration variable?

@guardrex

This comment has been minimized.

Copy link
Collaborator

@guardrex guardrex commented Mar 16, 2020

I don't know why from a framework-design perspective, but you can set a value in your app's config just like any ordinary string key-value and then read it into UsePathBase where you call it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Next sprint
  
To do
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.