Django & WordPress - wp-admin redirect issue - django

I have some specific configuration. I have a django site, and want to add an exiting blog which powered by Wordpress into django subfolder.
For security reasons I didn't want to install php stack aside django, and runned it in docker. Both are served by nginx, django also has a uwsgi.
In the Wordpress admin I configured both site and wordpress urls like this: https://mycoolsite.tld/blog
So, everything works well, except of some hardcoded redirect from https://mycoolsite.tld/blog/wp-admin to https://mycoolsite.tld/wp-admin.
The question - how can I play with nginx locations (and not tweak wordpress if possible) for accessing the /blog/wp-admin url?
Related nginx configs:
Django server - nginx blog location
location /blog/ {
rewrite ^/blog/(.*)$ /$1 break;
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:8080; # localhost(8080) <--> docker(80)
proxy_redirect off;
}
Wordpress nginx config (inside docker):
server {
listen 80 default_server;
root /var/www/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Related

Nginx redirect after slash in location

I have domine name https://example.com/API/, I wanted to redirect anything given after /API/ for example :
https://example.com/API/test to https://example.com/API/
Below is my Nginx conf
location #error {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
root /var/www/html/test/;
index index.html index.htm;
internal;
}
location ~*/api {
rewrite ^/api(.*) $1 break;
proxy_pass http://127.0.0.1:3100;
client_max_body_size 60M;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
error_page 502 #error;
}
That from the above example if /API/ gets 502 I am redirecting it to. PHP file is working fine, But if there is anything given after /API/test it is showing 404 not found.
You can have something of this sort:
server {
root /var/www/html; #your own values
server_name _; #website name
location #error {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
root /var/www/html/test/;
index index.html index.htm;
internal;
}
location /api {
proxy_pass http://127.0.0.1:3100;
client_max_body_size 60M;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
error_page 502 #error;
}
}
So basically you would have to use location /api {} directive and that would work.

How to run django and wordpress with nginx

I'm having problem with configuring my nginx.conf file to run django server on main domain and a WordPress site on domain.com/blog.
This is my configuration file which my WordPress dir is
/var/www/varzesh-kon/blog/:
upstream Main_Project_server {
server unix:/home/amirfarsad/django_env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 2n9l.s.serverhost.name;
client_max_body_size 4G;
access_log /home/amirfarsad/logs/nginx-access.log;
error_log /home/amirfarsad/logs/nginx-error.log;
location /static/ {
alias /home/amirfarsad/Main_Project/static/;
}
location /media/ {
alias /home/amirfarsad/Main_Project/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://Main_Project_server;
break;
}
}
location /blog/ {
root /var/www/varzesh-kon/blog/;
index index.php index.html index.htm;
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
root /var/www/varzesh-kon/blog/;
}
}
My django site works well but when I go to domain.com/blog, it gives me a 404 not found nginx page.
Try changing your bottom location block
location ^~ /blog {
root /var/www/varzesh-kon/;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
Explanation:
In case of the root directive, full path is appended to the root including the location part
In your case:
location /blog/ {
root /var/www/varzesh-kon/blog/;
The final path that nginx will derive is going to be:
/var/www/varzesh-kon/blog/blog
That's why its showing you 404 not found page
Solution:
Either use alias instead of root
or change root path to /var/www/varzesh-kon/
location /blog/ {
root /var/www/varzesh-kon/;
...
}
location /blog/ {
alias /var/www/varzesh-kon/blog/;
...
}
for more: wiki

How to run Django and Wordpress using Nginx and Gunicorn at the same domain?

I have a Django app that is running on a domain e.g. www.example.com
I want to create a Wordpress landing page, and point this landing page to the home url www.example.com and the wordpress admin site to www.example.com/admin or www.example.com/wp-admin. All the other URLs should be served by Django.
So, I want:
www.example.com -> wordpress
www.example.com/admin or www.example.com/wp-admin -> wordpress
All the other URLs to be served by Django
Till now, this is my Nginx configuration using Django:
upstream django_server {
server unix:/path/to/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.example.com example.com
client_max_body_size 4G;
access_log /path/to/nginx-access.log;
error_log /path/to/nginx-error.log;
location /static/ {
alias /path/to/static/;
}
location /media/ {
alias /path/to/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://django_server;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /path/to/static/;
}
}
Any help would be greatly appreciated.
WordPress uses an indeterminate set of URLs and so it is important to have a clear partition between that and the set of URLs available to Django. The best solution is to place WordPress into a subdirectory (which is surprisingly easy).
For example:
server {
...
# existing Django configuration
...
location = / {
return $scheme://$host/blog/;
}
location ^~ /blog {
alias /path/to/wordpress;
index index.php;
if (!-e $request_filename) { rewrite ^ /blog/index.php last; }
location ~ /wp-content/uploads/ { expires 30d; }
location ~ \.php$ {
if (!-f $request_filename) { rewrite ^ /blog/index.php last; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
...
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
if (!-f $request_filename) { rewrite ^ /blog/index.php last; }
expires 30d;
}
}
}
You will need to set the Site and Home URLs. See this document for details.
See this document for more.

Configure Django App as nginx location

I am trying to deploy a Django app along a static html website. The general www.example.com will provide the basic website. The URI /app should then proxy to a Gunicorn WSGI server. The nginx configuration in sites-enabled looks as follows:
upstream gunicorn {
server 127.0.0.1:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/website;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://gunicorn;
}
}
I am getting a 404 error when trying www.example.com/app. It seems the URI gets messed up somehow. The complete URL that is created is http://www.example.com/accounts/login/?next=/app/
Setting up the App alone exactly following this tutorial http://hackedexistence.com/project/raspi/django-on-raspberry-pi.html does work.
Is the setup I am aiming at even possible? And if yes, where is my misunderstanding of the location concept?
You need to set the SCRIPT_NAME header in your location directive.
proxy_set_header SCRIPT_NAME /app;
See this blog post for an example.

nginx + uwsgi custom 502 will not work

Hi I am trying to get a custom 502 page working on a website and can't seem to get it working.
Basically the way i'm testing it is I'm just stopping uwsgi and accessing the page and every time i get the default nginx 502 page. Can someone please explain to me how to get this working? I've been at this for over a week with 0 success. I have a file named 502.html in public_html and i can access it directly with http://ask.ploy.io/502.html but as soon as i stop uwsgi and try to access the main domain http://ask.ploy.io I get the default 502 page. Here is the vhost config:
### nginx vhost conf for ployio
server {
listen 80;
server_name ask.ploy.io www.ask.ploy.io;
access_log /usr/local/apache/domlogs/ask.ploy.io main;
error_log /home/ployio/access-logs/ask.ploy.io debug;
root /home/ployio/public_html;
index index.html index.htm index.php;
location /502.html {
root /home/ployio/public_html;
}
location ~ /\.ht {
deny all;
}
location / {
error_page 404 403 = #uwsgi;
log_not_found off;
error_page 502 /502.html;
root /home/ployio/public_html;
}
location #uwsgi {
internal;
uwsgi_pass unix:/home/ployio/.uwsgi/uwsgi.sock;
include /usr/local/nginx/conf/uwsgi_params;
}
location ~* ^.*\.php$ {
if (!-f $request_filename) {
return 404;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://204.61.223.114:8888;
}
location /cpanel {
rewrite ^/(.*) https://cpanel.ask.ploy.io:2083/$1 permanent;
}
}
If 502 it's the only error code you want to handle with a custom error page, you just need to be specific in the location:
location /502.html {
root /home/ployio/public_html;
}
Your current location is only being matched with the exact "/50x.html" path, which indeed does not exists in your server: http://ask.ploy.io/50x.html
It's also possible using nginx variables ($uri or something similar) to redirect all 50x errors to that root directory, but for your needs this should be enough.
The main issue is in the location #uwsgi section.. it never seems to handle the 502 return correctly.. maybe by design?
This is a working config
server {
listen 80;
server_name ask.ploy.io www.ask.ploy.io;
access_log /usr/local/apache/domlogs/ask.ploy.io main;
error_log /home/ployio/access-logs/ask.ploy.io debug;
root /home/ployio/public_html;
index index.html index.htm index.php;
location / {
uwsgi_pass unix:/home/ployio/.uwsgi/uwsgi.sock;
include /usr/local/nginx/conf/uwsgi_params;
}
error_page 502 503 504 #maintenance;
location #maintenance {
root /home/ployio/public_html_502;
rewrite ^(.*)$ /502.html break;
}
}
Make sure you put the 502.html in a new root and reference it there.. ps.. randall sucks