setting up reverse proxy to google.com while maintaining prior webservice hosted on apache

server {
    listen 80;
    server_name example.com;
    location / {
        rewrite ^/(.*)$ https://$host$1 permanent;
    }
}

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate      /etc/ssl/example.crt;
    ssl_certificate_key  /etc/ssl/example.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    location / {
        proxy_pass https://www.google.com;
        proxy_set_header Host www.google.com;
        proxy_set_header Referer https://www.google.com;

        proxy_set_header User-Agent $http_user_agent;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Accept-Encoding "";
        proxy_set_header Accept-Language $http_accept_language;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        sub_filter google.com example.com;
        sub_filter_once off;
    }
}

I am trying to use this, but 0.0.0.0:80 is in use by apache, but the question is how can I reverse proxy a request from bi.example.com to google.com while still having webservice.example.com redirect the request to example.com which hosts an API webservice. I don't think it can be done, but I am probably wrong.

I got this error when I tried to start nginx:

Dec 28 10:52:22 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address al...use)
Dec 28 10:52:23 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address alr...use)
Dec 28 10:52:23 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address al...use)
Dec 28 10:52:23 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address alr...use)
Dec 28 10:52:23 example.com nginx[27291]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address al...use)
Dec 28 10:52:24 example.com nginx[27291]: nginx: [emerg] still could not bind()

I checked the ports and it was being used by httpd, which corresponds to Apache.

To explain further, I have two CNAME redirect to example.com, and I want to redirect to a different page than Google, but this is just an example, so because both CNAME redirects to the same url, I don't think this is possible and a redirect from an url to another url to provide a SSL certificate to another link than Google.com is not possible if port 80 is used by Apache. Aside using a brand new server for this, are there any other possibilities? Right now, there's a CNAME redirect from bi.example.com to another url than Google, and I tried to set up a redirect with nginx by pointing the CNAME from bi.example.com to example.com instead of the other url.


Solution 1:

You cannot have two programs (Apache and nginx) listening on the same port, but you can have a single program (Apache or nginx) reverse proxy two separate hostnames to two different services, or even to serve content on one hostname and reverse proxy another one.

So the solution is to first decide on one of the two programs, either Apache or nginx, and then within that program set up two virtual hosts for the two hostnames which you redirected to it via CNAME records. One of those two virtual hosts can then reverse proxy to Google and the other one can serve (or reverse proxy to) your webservice.

The question of SSL certificates is a separate story. You can either configure your reverse proxy to use separate certificates for the two virtual hosts, or you can use a certificate which is valid for both of them.