Home > linux > How do I tell Nginx to pass requests to different apps based on the incoming url?

How do I tell Nginx to pass requests to different apps based on the incoming url?

February 8Hits:2
Advertisement

How do I tell Nginx to pass requests to different apps based on the incoming url?

For example, mybudgetapp.com and myshoppingapp.com both point to my server where both apps (8001, 8002) and Nginx (80) are running. When a user goes to mybudgetapp.com they should get a response from the app running on 8001. When a user goes to myshoppingapp.com they should get a response from the app running on 8002.

Here is an example that is close to what I want:

location / {   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:8001; } 

I have been reading this documentation but the terminology is throwing me off. http://nginx.org/en/docs/http/ngx_http_proxy_module.html

Is Nginx the right tool for this, or would Apache be a better fit?

Answers

Nope. Apache won't be better, however it can do the same work exactly. The canonical way of fullfilling your wish is to create different servers in nginx, and proxy to different backends according to the Host HTTP header, along with passing that header, is necessary:

server {
    listen 80;
    server_name myshoppingapp.com;
    proxy_set_header Host $host;
    location / {
        proxy_pass http://firstapp;
    }
}

server {
    listen 80;
    server_name mybudgetapp.com;
    proxy_set_header Host $host;
    location / {
        proxy_pass http://secondapp;
    }
}

Tags:linux, nginx

Related Articles

  • How do I tell Nginx to pass requests to different apps based on the incoming url?February 8

    How do I tell Nginx to pass requests to different apps based on the incoming url? For example, mybudgetapp.com and myshoppingapp.com both point to my server where both apps (8001, 8002) and Nginx (80) are running. When a user goes to mybudgetapp.com

  • Nginx Re-write: Different URL based on incoming URL format January 5

    This question already has an answer here: nginx support for .htaccess / rewrite rules? Differences from Apache? 4 answers I have a server that will have 2 different types/formats of requests coming in. Both request URL's will start with: http://myred

  • How to let nginx simply pass a request to a forwarding proxy server?

    How to let nginx simply pass a request to a forwarding proxy server?September 20

    'm behind my corporate forwarding proxy. When I test my application locally, for some requests, I want to configure nginx to proxy_pass those requests to some external domain servers. below is my configuration, while this doesn't work. I'm always get

  • Nginx proxy pass depend on post request dataFebruary 1

    I need help into nginx proxy pass configuration. Suppose "x" partner login then nginx proxy pass url should be proxy_pass http://192.168.1.1 and for "y" partner login it should be proxy_pass http://192.168.1.2 How i can do the nginx co

  • Nginx Retry of Requests ( Nginx - Haproxy Combination )March 11

    I wanted to ask about Nginx Retry of Requests. I have a Nginx running at the backend which then sends the requests to HaProxy which then passes it on the web server and the request is processed. I am reloading my Haproxy config dynamically to provide

  • Make nginx to pass hostname of the upstream when reverseproxyingMay 23

    I run several docker containers with hostnames: web1.local web2.local web3.local Routing to these done based on hostname by nginx. I have a proxy in front of this setup (on different machine connected to internet) where I define upstream as: upstream

  • Nginx forward all requests to another domainOctober 24

    I have been struggling to get this whole setup to work. I've found bits and pieces on the net but nothing that solves all of my requirements. I need to do this to make IE8 work, so unfortunately my hands are tied a little. I can move some endpoints a

  • 502: proxy: pass request body failedJune 2

    Sometimes I get the following error (in apache's error.log) when viewing my site over https: (502)Unknown error 502: proxy: pass request body failed to xxx.xxx.xxx.xxx:443 I'm not entirely sure what this is and why it happens, it's also not consisten

  • Nginx, awstats and request time

    Nginx, awstats and request timeOctober 4

    How can I tune nginx to write request time to logs such that awstats can show time taken for each request? Also, how can I see requests by time taken in awstats? This is the current format log_format main '$remote_addr - $remote_user [$time_local] "$

  • proxy: pass request body failedJanuary 3

    I'm trying to enable proxy virtualhost: <VirtualHost *:80> ServerName xxxxx.domain.tdl SSLProxyEngine On SSLProxyCheckPeerCN on ProxyPass / https://localhost:1234 ProxyPassReverse / https://localhost:1234 </VirtualHost> But i've an 500 err and

  • mod_proxy: pass path from original request to proxied appApril 11

    I'm struggling with reverse proxying an app running in a Tomcat server on a closed port behind Apache. For convenience, say that I want to reverse proxy requests for 'http://mydomain/apps/my_app/' to the Tomcat app at 'http://localhost:8082/my_app/'

  • Nginx, proxy passing to Apache, and SSLDecember 8

    I have Nginx and Apache set up with Nginx proxy-passing everything to Apache except static resources. I have a server set up for port 80 like so: server { listen 80; server_name *.example1.com *.example2.com; [...] location ~* \.(?:ico|css|js|gif|jpe

  • Apache mod_rewrite : RewriteRule to pass requests tomcat application January 27

    This question already has an answer here: Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask 5 answers I am having difficulty defining a correct RewriteRule to

  • (103) Software caused connection abort: proxy: pass request body failedFebruary 16

    The following errors are being logged in our proxy Apache logs while processing the request with Tomcat Server: (103)Software caused connection abort: proxy: pass request body failed proxy: pass request body failed We've a Apache reverse proxy which

  • Nginx and passenger request timeout (nginx status code 499)March 19

    DISCLAIMER: I have read numerous posts here, on stackoverflow and other pages and I could not come up with a solution, so hopefully this helps. We are running a Rails App on Passenger together with nginx and HAProxy. Passenger: 4.0.7 Nginx: 1.4.1 The

  • pass request body failed to 127.0.0.1:8001December 8

    we are running a Debian with Apache + NodeJS backend, and all of the sudden we see this in our log files: [Tue Dec 08 11:14:45.123275 2015] [proxy:error] [pid 21479:tid 139720436733696] (32)Broken pipe: [client 188.118.47.225:50187] AH01084: pass req

  • Pass request to every Swig template using ExpressJanuary 21

    Is it possible to pass request/req object to every template I render, so I wouldn't have to pass it as local in every render() method? --------------Solutions------------- You can use res.locals for that. Insert the following middleware somewhere bef

  • Can not pass request parameter to controller from route in laravel 5February 11

    I have public function checkCookie(Request $request) { // do something } in my controller and I am trying to call it from routes.php with Route::get('mypage',function(){ $a=new App\Http\Controllers\indexController; $a->checkCookie(); }); but I can't

  • Remove query strings only on requests requiring PHP (ignoring static files and certain urls)August 5

    One of my sites keeps getting spammed with fake URL requests containing query strings. I have caching in place but unfortunately query strings are ignored. Now every time I get a flood of query string requests my server gets nailed due to an influx o

  • Nginx as reverse proxy for Google App Engine applicationMay 30

    I want to use nginx as a reverse proxy for Google App Engine application to support naked domain as described here. If I use this scheme, will all the traffic go through the server that hosts nginx or client will connect to App Engine directly? Can i

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.826 s.