I have a Django app on a Hetzner server and a Wordpress site on a Hostinger server.
I want to configure Nginx on my Hetzner server to serve the Django app and when it requests the /route-name route, it serves a Wordpress site from the Hostinger server.
I’ve already allowed the IP address of my Hetzner server to access the Wordpress site.
Hetzner server is running Nginx and Hostinger is running Apache2 if that’s relevant.
I know there are quite a few questions and answers regarding configuring Nginx to serve Django and Wordpress but I’ve been scouring this forum and the Internet for several hours now with no solution found for my problem. I suspect it may have to do with my Nginx config, but after trying out several configurations accepted on Stackoverflow and elsewhere, I can’t seem to make it work.
This is what I currently have for my Nginx config:
server {
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /var/www/example.com/static/;
}
location /media/ {
alias /var/www/example.com/media/;
}
# Reverse proxy for /route-name
location /route-name/ {
proxy_pass http://<IP_ADDRESS_OF_HOSTINGER_SERVER/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
include proxy_params;
proxy_redirect off;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
I don’t have much experience setting up Nginx and I’ve just been following tutorials and whatnot.
My current config returns 403 when I go to example.com/route-name when I’m expecting it to show the Wordpress site hosted on the Hostinger server.
I’ve already allowed the IP address of my Hetzner server to access the Wordpress site.
My quick research says 403 can be caused by misconfiguration as well such as the .htaccess file but I’ve checked the file and it does contain this line: allow from <IP_ADDRESS_OF_HETZNER_SERVER>.
I believe the other lines are the default settings although since I’m not familiar with how Apache2 servers are set up, I can’t rule out the possibility of misconfiguration as well.
Thanks for pointing me to the logs, completely forgot about them.
My server setup has changed since and I’m now hosting both the Django project and the Wordpress site on my Nginx server. Unfortunately I do not understand what I’m doing wrong.
I was still getting 403 but after reading directory index of "/path/to/wordpress/public" is forbidden I changed my config to what it is below.
Now I am getting 404 and this is what my config looks like now:
upstream php-handler {
server unix:/var/run/php/php8.3-fpm.sock;
}
server {
index /path/to/django;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /var/www/example.com/static/;
}
location /media/ {
alias /var/www/example.com/media/;
}
# Reverse proxy for /freebies
location /route-name/ {
root /path/to/wordpress/public;
index index.php;
try_files $uri /index.php?$args;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass php-handler;
}
}
location / {
include proxy_params;
proxy_redirect off;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
Now I don’t understand why Nginx cannot find my index.php although I’ve clearly specified where to look for it. Please help, thanks!
Sorry, I haven’t worked with a Wordpress deployment in about 15 years or any php-based deployment in about 8.
The only suggestions I have are:
Find the documentation for Wordpress deployments under nginx, especially when it comes to deploying it within a sub-path.
Check all the possible logs (nginx, php, Wordpress-specific) for any clues.
Simplify the situation by getting it working without the Django stuff. Remove all the Django-related location directives to help highlight possible configuration conflicts.