1

I have a Ubuntu server running Apache, which hosts a Wordpress site, which is accessible using HTTP. There's also Nginx installed that hosts a Django site that's only accessible using HTTPS.

Now, what I'd like to accomplish is that example.com and www.example.com would go to the Wordpress hosted by Apache, while api.example.com would go to the Django hosted by Nginx. This does work at the moment if I just try to access the main page at api.example.com, but there's one weird catch: When I try to access api.example.com/admin (Django admin panel), the page gets redirected to the Wordpress admin login page at www.example.com/wp-admin. Why is that?

I've even tried to change the Django admin panel URL to something else. This in turns leads to Wordpress 404 page ("Oops, that page could not be found!").

So, it seems that the portion of the URL after the domain gets hijacked by Apache, even though it's on the different subdomain. What causes this, and how can I fix it?

The Apache site is configured like this:

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/wp

    <Directory /var/www/wp>
        Options Indexes FollowSymlinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

And the Nginx site is configured like so:

upstream app_server {
    server unix:/tmp/gunicorn.sock fail_timeout=0;
}

server {
    listen 443 ssl;
    charset utf-8;
    server_name api.example.com;

    ssl on;
    # Here be a lot of SSL configs

    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://app_server;
    }

    location /static {
        root /var/www/django/static;
    }
}

Finally, my DNS record is set up like this:

A example.com 1.2.3.4
CNAME api example.com
CNAME www example.com

Thanks for your help in advance. Let me know if you need more information.

1 Answer 1

0

You're probably entering these URLs in this shorthand form in your web browser, and it's not guessing what you want correctly; it probably remembered from history that when you type "api.example.com" that it should use HTTPS, but for new URLs "api.example.com/whatever" it doesn't have history so it tries HTTP first.

If you're using the same IP address for all of this, regardless of the fact that https://api.example.com/* is going to the process listening on the HTTPS port (443), http://api.example.com/* will still go to the process listening on the HTTP port (80).

If you want to avoid this while keeping the same IP address, set up an api.example.com virtual host in the HTTP-only server that redirects every request it receives to HTTPS.

To actually split them up the hard way, add a new IP address for api.example.com to the server and make the two HTTP(S) servers listen each on their own IP, and update DNS so api.example.com points to the new IP address.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .