Home > nginx > Nginx forward all requests to another domain

Nginx forward all requests to another domain

October 24Hits:3
Advertisement

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 around, but probably not too much.

I have a users api which sits at https://api.foo.com/1.0/users. That endpoint accepts all request types along with parameters, and dynamic segments. Below are some samples

I want to configure nginx to accept requests from another domain and path and seamlessly forward them to those endpoints. For example

In order to accomplish this, I need request parameters as well as form data to be forwarded. Basically the whole request needs to be forwarded in tact and then return. Also note that I have a couple of different subdomains so awesome is but one of a couple of names I want to change.

The pattern is this

http://example.com/api/<subdomain>/<details> -> https://<subdomain>.foo.com/1.0/<details> 

My biggest struggle is keeping the exactly the same, along with passing different kinds of requests.

my efforts I've tried a number of things, but probably the closest I've got to is this

location ~ ^/api/(.*)/(*.) {      resolver 8.8.8.8;     proxy_pass https://$1.foo.com/1.0/$2$is_args$args;     proxy_set_header X-Real-IP $remote_addr;     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;     #proxy_set_header X-Host $host; }  **The `X-Host` directive doesn't work for me hence the comment 

This works for the first two cases, but not the third. Also, I haven't got the POST request to work with this. Mostly I get 502 Bad Gateway errors. It seems to me that the doesn't map completely instead it cuts off at the first '/' it receives. Adding another location directive with 3 regex params also hasn't helped me.

I'd really appreciate any help on this topic as I'm totally stuck as to what to do next.

Answers

Your regex is incorrect, you sould use location ~ ^/api/([^/]+)/(.*)$ instead because I don't see how the PCRE lib can choose $1 as the subdomain, its content will likely be awesome/users instead of awesome.

Tags:nginx

Related Articles

  • 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

  • Apache and Nginx - Forwarding .htaccess requests to Apache?December 3

    A really simple one but it's alluding me entirely. I've set up nginx as a proxy for Apache and at the moment it's only set to allow requests for certain files through to Apache, the config line is below. Problem I'm having, I need it to send .htacces

  • nginx: redirect all requests from one domain to other with a certain scriptNovember 19

    So what I have: nginx php-fpm What I need: Main domain: example.com Other domain: otherexample.com In main domain we have script, which can do some logic and need when we request to otherexample.com do request to example.com/somescript.php?domain=oth

  • How can I make Nginx drop SSL requests from wrong domain?May 3

    So I've got a server with a single IP and a lot of websites, all hosted under nginx. Here's a very simplified reduction of my actual setup: server { listen 80; listen 443 ssl; server_name ssl.example.com; #... } server { listen 80; server_name nossl.

  • dnsmasq forward dns requests + add domain suffix for not fqdnFebruary 25

    I want to use dnsmasq as a DNS forwarder: for fully qualified domain names it should forward request without change; for not fully qualified name it should add domain to the name and forward (searching ubuntu -> forward searching ubuntu.mydomain.com)

  • Windows Server 2008R2 DNS Server forwards second request that includes local domain name

    Windows Server 2008R2 DNS Server forwards second request that includes local domain nameMarch 7

    I recently found out that the DNS server in my Windows network (Windows Server 2008 R2 also DC) forwards two requests to the external DNS servers. If I enter a command like: nslookup google.com dc1.mycompany.local The domain controller receives it an

  • When does nginx's proxy_pass return 301 and when does it forward the request?February 10

    When using a nginx server on a vagrant (hosted by virtualbox) i used this config ... server { port_in_redirect on; listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /path/to/public; index index.html index.htm; # Make site acce

  • On a Windows workstation, how can I forward DNS requests for for a specific domain?January 31

    How can I achieve the following on a Windows 10 machine: DNS requests for a particular domain name mydomain.com should be sent to a specific IP address. DNS requests for other domain names should be sent to the DNS servers set by DHCP.

  • Forwarding HTTP Request with Direct Server ReturnFebruary 14

    I have servers spread across several data centers, each storing different files. I want users to be able to access the files on all servers through a single domain and have the individual servers return the files directly to the users. The following

  • Nginx config: two applications under same domain November 7

    This question already has an answer here: How can I get around using alias in nginx? 1 answer I have two applications: HTML+JS frontend and PHP backend. I would like to set Nginx up so that both are served from the same domain. Requests to the backen

  • Simplest way to forward HTTP requests to a different serverDecember 13

    I have a mobile app that needs to communicate with a third-party server over an HTTP API. But the third-party server can be accessed only from an approved IP addresses, meaning that I need to put my server (whose IP has been approved) between the app

  • Forwarding SOAP requestsDecember 21

    I ran into the following situation. A client provides a soap service on port 80. It's unknown how their clients access the service exactly but it assumed that they have the uris hardcoded in their scripts. Currently we are developing a new website fo

  • Forwarding HTTP requests to VMs behind a virtual networkJuly 28

    I'm trying to set up a virtual machine host (currently running ubuntu + KVM) which will host a number of VMs for different applications. For example, we have a wiki running on one VM and a web application running on another. Host / \ VM1 VM2 Internal

  • Forward Http Request to different machineJanuary 20

    I am working on a MAC machine and expected to use some of the Webservices which are present on a different network. The general way to access these webservices is through VPN to that network and then access. For some known reason I am not able to (su

  • 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

  • how to forward IP request to a specific portOctober 22

    I have 2 servers the first (SRV01) is running Bind and other web app the second (SRV02) is running 2 server Minecraft (^^) in Bind I have 2 A recording for the 2 server MC s1.domain.tld A SRV02IP s2.domain.tld A SRV02IP the 2 MC serv are running on 2

  • Forward PTR request if not foundNovember 7

    I am attempting to find a way to forward PTR request, if not found in local master zones. I'm transitioning a network from one domain (and dns servers) to another, which is easy for A records - but a mess for PTR, as ips haven't changed. How can I se

  • Configuring bind9 to forward subdomain requests to internal DNS serverSeptember 16

    I'm attempting to implement a trivial loadbalancer using a DNS server that returns a random IP for a given subdomain. I have the top level domain which is currently handled by bind9, and I'd like to keep it this way. Is there a way I can configure bi

  • Forward DNS requests from routerSeptember 28

    I've got a server behind a router and I'm trying to use my server as my DNS and DHCP service. I have them configured correctly, and I can connect out through, but I cannot seem to get my router to forward DNS requests to the server. In my router sett

  • Forward web request to another computerFebruary 14

    I have one notebook and two servers: ip = 172.17.0.2 (I have installed nginx here) ip = 172.17.0.3 (There is nothing here, but I can ping 172.17.0.2 from here) I open browser on my notebook, open http://172.17.0.2 and get web page from Nginx. Now I w

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