简体   繁体   中英

Using nginx as a reverse proxy to IIS server

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?

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.

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;
}

Try to use this directive

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

and the same block for 2nd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM