How to run django and wordpress with nginx - django

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

Related

Nginx not routing the request properly

server {
listen 80 default_server;
server_name http://localhost;
server_tokens off;
root /usr/share/nginx/html;
location = /home {
index index.html index.htm;
try_files $uri /index.html;
}
location = /user {
index index.html index.htm;
try_files $uri /index.html;
}
location = /login {
index index.html index.htm;
try_files $uri /index.html;
}
location = /signup {
index index.html index.htm;
try_files $uri /index.html;
}
location ^~\/verify\/\?token=([A-Za-z0-9-_]*\.[A-Za-z0-9-_]*\.[A-Za-z0-9-_]*$) {
proxy_pass 'http://localhost:8000';
}
location ~ "^\/([0-9a-zA-Z+=-]{7,})$" {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass 'http://localhost:8000';
}
}
I have this nginx.conf file, I want to route /verify/?token=something.something.something(the value of token is jwt) to the backend.
But getting 404 error while trying to access the endpoint, all other routes are working properly.

problem with serving static files of django on server with nginx

Hi everyone I have changed default file of nginx where is in /etc/nginx/sites-available/default as below
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80;
location /static/ {
root /home/django/chistaa/chistaa/settings;
try_files $uri =404;
}
location / {
try_files $uri #send_to_django;
}
location #send_to_django {
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://django;
}
}
but it doesnt load my static files.
What did I wrong?!

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.

How to configure nginx to run Wordpress and Django

I've looked at a couple similar question, but they didn't seem to be any help. I've got a vultr.com instance running a Wordpress in the default 1 click configuration. Centos6 if that matters. This is (and I would like it to remain) in the root (www.mysite.com). I would like to have my Django app running in www.mysite.com/crossfaded/. I don't want any downtime for my current server, so there is no domain name associated with the server yet. I'm trying to do this just using the IP.
The Wordpress site is working fine.
I've been following the guide here as then tried this one, but when I navigate to http://ip.add.re.ss/crossfaded/media/apple.jpg in my browser, I get a 404 from nginx. Going /crossfaded/media/ gives me a 403 from nginx and /crossfaded/invalidpath/ gives me a 404 served by Wordpress, so something is happening with the routing. I did chmod 777 apple.jpg on the off-chance it was a permissions issue, but that didn't do anything.
I have a hunch I've got the syntax of the location block muddled, but I'm really not sure.
wordpress_http.conf
upstream php-handler-http {
server 127.0.0.1:9000;
#server unix:/var/run/php5-fpm.sock;
}
server {
listen 80 default_server;
server_name _;
#server_name wordpress.example.com;
root /var/www/html/;
index index.php;
# set max upload size
client_max_body_size 2G;
fastcgi_buffers 64 4K;
access_log /var/log/nginx/wordpress_http_access.log combined;
error_log /var/log/nginx/wordpress_http_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args ;
}
location ^~ /wp-admin/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd/wpadmin;
location ~* \.(htaccess|htpasswd) {
deny all;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-handler-http;
fastcgi_read_timeout 60s;
}
}
location ~* \.(htaccess|htpasswd) {
deny all;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-handler-http;
fastcgi_read_timeout 60s;
}
# set long EXPIRES header on static assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
access_log off;
}
location ~ /crossfaded/static/ {
alias /root/crossfaded/static/;
}
location ~ /crossfaded/media/ {
alias /root/crossfaded/media/ ;
}
}
There were two problems:
I needed a caret (^) in the location of the alias, so:
location ^~ /crossfaded/static/ {
alias /root/crossfaded/static/;
}
location ^~ /crossfaded/media/ {
alias /root/crossfaded/media/ ;
}
The server defaulted ~/ to /root/. Nginx needs r-x permissions to all directories from / to the file in question. It did not have that. Moved the project directory to /home/.

Django & WordPress - wp-admin redirect issue

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;
}
}