18

I have multiple ASP.NET applications running on a single IIS server with different ports for each application.

I have installed nginx on the same server so that my clients can access all my applications only using port 80.

Nginx is running all right on port 80. My individual ASP.NET applications are also up and running.

I made these changes in nginx conf file

    location /students/ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:84;
    }
    location /registration/ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:82;
    }

Then I restarted nginx and typed the url http://127.0.0.1/students/ in my browser. Nginx served a 404 page.

I made no other changes to the conf file.

What I am doing wrong?

1
  • That config looks perfectly correct; we have very similar config at my work, using proxy_pass, inside a location block, without a URI portion, to pass requests to different servers with URI unchanged. Are you able to verify whether Nginx is entering the location block? Commented Oct 29, 2015 at 0:48

3 Answers 3

16
+25

I believe that the problem you are having is related to the start of the URL path. Does the URL http://120.0.0.1:84/students/ return the page, or a 404? If you are expecting to go to http://127.0.0.1:80/students/ and see the page at http://127.0.0.1/, you will find that nginx does not transform the path for you with this configuration. Rather, it looks for exactly the same path at the proxied server.

You need to put the / on the end of the URL in the proxy_pass directive:

location /students/ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:84/;
}

This is a subtle but important gotcha in nginx configs! If you don't include the backslash, http://127.0.0.1:84 will be treated as a server location. If you do have the backslash, it will be regarded as a URL, and it will replace everything in the proxy URL up to the 'location' part.

Sign up to request clarification or add additional context in comments.

9 Comments

If I browse 127.0.0.1:84/students I will get my page, because IIS is listening on 84. What I want is that if I browse 127.0.0.1/students then nginx should rewrite the url to 127.0.0.1:84. Is this possible in nginx?
@Shuaib I believe this is exactly what happens with the config above.
i am still getting a 404 page served by nginx.
I am going to 127.0.0.1/students. I do not want to go to 127.0.0.1:84/students ... IIS can server that and I do not need nginx @jwg.
Are you going to http://127.0.0.1/students or http://127.0.0.1/students/ ? Sorry, StackOverflow removed the slash at the end of the URL, this was the whole point of the question!
|
1

If you want to transform IIS 127.0.0.1:84/students to nginx 127.0.0.1/students . try below code..

location /students {
   proxy_set_header X-Real-IP  $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_set_header Host $host;
   proxy_pass http://127.0.0.1:84/students;
 }
location /registration {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:82/registration;
}

Comments

-5

Try to use this directive

 upstream http://localhost {
     server 127.0.0.1:84;
 }

and the same block for 2nd

2 Comments

Nginx gives me the error ... upstream is not allowed here
Please add some explanation to your answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.