Deploying Gunicorn using subpath in NGINX - python

According to this tutorial, there is section below that says this.
You must now configure your web proxy to send traffic to the new Gunicorn socket. Edit your nginx.conf to include the following:
/etc/nginx/nginx.conf:
...
http {
server {
listen 8000;
server_name 127.0.0.1;
location / {
proxy_pass http://unix:/run/gunicorn/socket;
}
}
}
...
What I want to do is use location /mysubpath. How should I do it?
I'm using mysubpath since I have 2 different projects running on the same server.
Something like this.
...
http {
server {
listen 8000;
server_name 127.0.0.1;
location /mysubpath {
proxy_pass http://unix:/run/gunicorn/socket;
}
}
}
...
Usually, we do proxy_pass http://someip:someport/; but in my case, it might require a different approach. Can someone please help.

Related

Nginx error_page 404 proxy_pass

Can someone please help me on this.
I am configuring Nginx with flask app using gunicorn in a host using proxypass. While accessing that path i am getting 404 Page not found error.
Here is my Nginx config:
server {
listen 80;
server_name xxxx.com www.xxxx.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xxxx.com www.xxxx.com;
ssl_certificate xxxxxx;
ssl_certificate_key xxxxxx;
location / {
include proxy_params;
proxy_pass http://unix:/xxxx/app.sock;
}
location = /dev{
proxy_pass http://unix:/xxxx/app.sock;
include proxy_params;
}
}
If i configure in home(xxxx.com) and access the home its working but when i tried to configure in path(xxxx.com/dev) and access this http://xxxxx.com/dev getting 404 error. My actual application is in /home/path/to/folder/app.sock
Please help me on this.
Thank you.
location /dev {
include proxy_params;
rewrite ^/dev(.*)$ /$1 break;
proxy_pass http://unix:/path/to/folder/dev.sock;
}
Works for me.
Thank you.

How to run wordpress on localhost and django on localhost/product?

I have developed a wordpress website for a product.And the product is made using django.So after the user logs in ,i have to redirect him to the django project.I have to achieve this on the same domain.I have nginx installed.
You can try this
www.egwordpress.com (your Wordpress)
egproduct.egwordpress.com (your Django app)
NGinx config
server {
listen 80 default_server;
server_name www.egwordpress.com;
location / {
root /path/to/your/wordpress;
index index.html;
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name egproduct.egwordpress.com;
location /api/ { # your django product app
proxy_pass http://127.0.0.1:5000; # change port following your Django config
}
}

NGINX default page is being shown instead of Flask app

I have displayed my NGINX configuration below. One of my Flask applications work (the one above), however the one below does not. The weird thing is, they were both working the other day, now the one below (example2) just returns the NGINX default page.
File structure:
/Signup/
/static/
/assets/
/bootstrap/
/css/
/fonts/
/img/
/js/
/templates/
register.html
main.py
NGINX Config:
server {
listen 80;
listen [::]:80;
server_name example1.domain.net;
location / {
proxy_pass http://127.0.0.1:5000;
}
}
server {
listen 80;
listen [::]:80;
server_name example2.domain.net;
location ^~ /static/ {
proxy_pass http://127.0.0.1:6000;
include /etc/nginx/mime.types;
root /root/Signup/;
}
}
Example 1 works as it should, example2 (second server block does not).
Edit: Thought I should note, I have no prior experience with NGINX, please don't assume.

Django application with nginx and gunicorn only showing welcome page rest all pages showing 404 error

I tried to deploy a sample django application in amazon ec2 server with the help of nginx and gunicorn. I added proxy pass in nginx. After I run the server and accessing my IP I was able to view the welcome to django page. But when I navigate to some other urls, say admin its shows 404 not found error.
How to fix this error.
Nginx config:
upstream app {
server 127.0.0.1:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name IP;
location /static/ {
root /home/ubuntu/workspace/business;
}
location / {
proxy_pass http://app;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
You need change this:
location / {
proxy_pass http://app;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
For this:
location / {
proxy_pass http://gunicorn:8888; #use your gunicorn port
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
The problem was with the line
try_files $uri $uri/ =404;
This line is causing every url except the main url to route to 404 page.
I removed it and hence working.
Thanks to #Richard Smith for mentioning it in the comment

nginx intercepting google oauth redirect

I have my django app setup on a gunicorn server that is being proxied by nginx(also used for static files), and nginx is "intercepting" the GET request with the credentials code from Google! Why is nginx stealing the request instead of passing it to gunicorn to be processed?
here is my api info for my web application:
Client ID:
67490467925-v76j4e7bcdrps3ve37q41bnrtjm3jclj.apps.googleusercontent.com
Email address:
67490467925-v76j4e7bcdrps3ve37q41bnrtjm3jclj#developer.gserviceaccount.com
Client secret:
XquTw495rlwsHOodhWk
Redirect URIs: http://www.quickerhub.com
JavaScript origins: https://www.quickerhub.com
and here is the perfect GET request being stolen by nginx:
http://www.quickerhub.com/?code=4/bzqKIpj3UA3bBiyJfQzi3svzPBLZ.QoB_rXWZ6hUbmmS0T3UFEsPMOFF4fwI
and of course sweet nginx is giving me the "Welcome to nginx!" page...
Is there a way to tell nginx to pass these requests on to gunicorn? Or am I doing something incorrectly?
Thanks!
NGINX vhost config:
upstream interest_app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/webapps/hello_django/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name quickerhub.com;
client_max_body_size 4G;
access_log /webapps/hello_django/logs/nginx-access.log;
error_log /webapps/hello_django/logs/nginx-error.log;
location /static {
root /webapps/hello_django/interest/;
}
location /media {
root /webapps/hello_django/interest/;
}
location /static/admin {
root /webapps/hello_django/lib/python2.7/site-packages/django/contrib/admin/;
}
location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;
# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://interest_app_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/hello_django/interest/templates/;
}
}
you have
server_name quickerhub.com;
the get request is coming back to
http://www.quickerhub.com/?code=4/bzqKIpj3UA3bBiyJfQzi3svzPBLZ.QoB_rXWZ6hUbmmS0T3UFEsPMOFF4fwI
quickerhub.com != www.quickerhub.com and so nginx is falling through to serving the default page (for when it can't find a vhost).
all you need to do is use
server_name www.quickerhub.com quickerhub.com;
or even better, add this to "canonicalise" all your urls to the version without www.
server {
server_name www.quickerhub.com;
expires epoch;
add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
rewrite ^ http://quickerhub.com$request_uri permanent;
}

Categories