nginx + uwsgi custom 502 will not work - django

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

Related

Getting 504 Gateway Time-out nginx/1.18.0 (Ubuntu) in django nginx

I am facing problems launching my django app in digitalocean. I have done all the needful but I am quite sure I made error some where. Below is my nginx configuration file. What could be the problem . Thanks alot .
PLease I would also love to know how to access nginx error log file through command line.
server {
server_name server_Ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/username/myproject/smsdigo;
}
location /media/ {
root /home/username/myproject/smsdigo;
}
location /locale/ {
root /home/ebong/myproject/smsdigo;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
server {
listen 80;
client_max_body_size 3000M;
client_body_buffer_size 3000M;
client_body_timeout 120;
}
Problably you need increase fastcgi timeout directives:
Try 60s or more. For example:
server{
fastcgi_read_timeout 60s;
fastcgi_send_timeout 60s;
}

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.

Nginx Gunicorn one ip multiple django sites in folders

thanks for reading my question.
I'm trying to serve multiples Django sites on their own folders in one server without domain (only IP address) using Gunicorn and Nginx. Something like that:
20.20.20.20/demos/myapp1/ --> Django app
20.20.20.20/demos/myapp2/ --> Django app
20.20.20.20/demos/myapp3/ --> Django app
I have tested a lot of settings but I can't make it work. When i tried to load URL 20.20.20.02/demos/myapp1/ i get a 404 not found error :(
Example one site nginx conf:
upstream app1_server {
server unix:/webapps/myapp1/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 20.20.20.20;
keepalive_timeout 5;
client_max_body_size 4G;
location /static/ {
alias /webapps/myapp1/static/;
}
location /media/ {
alias /webapps/myapp1/media/;
}
location /demos/myapp1/ {
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_pass http://app1_server;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/myapp1/static/;
}
}
What is wrong with my myapp1.conf file?
For the record, if i change "location /demos/myapp1/" to "location /" my first app works, but the others apps still not working.
Thanks in advance.
Edit 1:
Checking my problem.. For now i found a solution. Rewrite rule:
location /myapp1/ {
rewrite ^/myapp1(.*) $1 break;
try_files $uri #proxy_to_app;
}
Is a good solution? My Django apps broken their urls :(
Well, reading about Nginx, i solved my problem in 4 steps:
Using rewrite rule like my edit post.
Listen each app in diferent port, like this:
server {
listen 81;
server_name 20.20.20.20;
location /demos/myapp1/ {
rewrite ^/demos/myapp1(.*) $1 break;
try_files $uri #proxy_to_app;
}
...
}
server {
listen 82;
server_name 20.20.20.20;
location /demos/myapp2/ {
rewrite ^/demos/myapp2(.*) $1 break;
try_files $uri #proxy_to_app;
}
...
}
server {
listen 83;
server_name 20.20.20.20;
location /demos/myapp3/ {
rewrite ^/demos/myapp3(.*) $1 break;
try_files $uri #proxy_to_app;
}
...
}
Reload Nginx
sudo service nginx restart
Test it:
http://20.20.20.20:81/myapp1/
http://20.20.20.20:82/myapp2/
http://20.20.20.20:83/myapp3/
If you have a better way to solve my problem, please let me know!

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.

Nginx + Django + Phpmyadmin Configuration

I've migrated my server to amazon ec2, and trying to set up the following environment there:
Nginx in the front serving static content, passing to django for dynamic content. I also would like to use phpmyadmin in this setting.
I am not a server admin, so I simply followed a few tutorials to make nginx and django up and running. But I've been working for two days now trying to hook phpmyadmin to this setup, with no avail. I am sending my current server configuration now, how can I serve phpmyadmin here?
server {
listen 80;
server_name localhost;
access_log /opt/django/logs/nginx/vc_access.log;
error_log /opt/django/logs/nginx/vc_error.log;
# no security problem here, since / is always passed to upstream
root /opt/django/;
# serve directly - analogous for static/staticfiles
location /media/ {
# if asset versioning is used
if ($query_string) {
expires max;
}
}
location /admin/media/ {
# this changes depending on your python version
root /path/to/test/lib/python2.7/site-packages/django/contrib;
}
location /static/ {
# if asset versioning is used
if ($query_string) {
expires max;
}
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://localhost:8000/;
}
# what to serve if upstream is not available or crashes
error_page 500 502 503 504 /media/50x.html;
}
This question should rightly belong to http://serverfault.com
Nevertheless, the first thing you ought to do is to configure a separate subdomain for your phpmyadmin for ease of administration.
So there will be two apps running with nginx as reverse proxy, one nginx server for your above django app and another server (also known as virtualhost) for your phpmyadmin with a configuration similar to this:-
server {
server_name phpmyadmin.<domain.tld>;
access_log /srv/http/<domain>/logs/phpmyadmin.access.log;
error_log /srv/http/<domain.tld>/logs/phpmyadmin.error.log;
location / {
root /srv/http/<domain.tld>/public_html/phpmyadmin;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /srv/http/<domain.tld>/public_html/phpmyadmin;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/http/<domain.tld>/public_html/phpmyadmin/$fastcgi_script_name;
include fastcgi_params;
}
}
Each of your server configuration can point at different domain names via the server_name configuration. In this example, server_name phpmyadmin.<domain.tld>;
Here's an example taken from http://wiki.nginx.org/ServerBlockExample
http {
index index.html;
server {
server_name www.domain1.com;
access_log logs/domain1.access.log main;
root /var/www/domain1.com/htdocs;
}
server {
server_name www.domain2.com;
access_log logs/domain2.access.log main;
root /var/www/domain2.com/htdocs;
}
}
As you can see, there are two declarations of server inside the large http brackets. Each declaration of the server should contain the configuration you have for django and another for the configuration of phpmyadmin.
2 "virtual hosts" ("server" instances) taken care by nginx.