NGINX default page is being shown instead of Flask app - python

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.

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.

Deploying Gunicorn using subpath in NGINX

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.

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

Serve static files through Nginx and uwsgi

I'm struggling to let Nginx serve static files and tried out about every configuration option I could find. Here's the app's basic structure.
ROOT
|_ static
|_css/
|_js/
|_ app.py
I've read Nginx docs about it, but the files do not seem to cache.
Here's my /etc/nginx/nginx.conf:
user www-data;
events {
worker_connections 64;
}
http {
gzip on; # Enables compression
gzip_types
"application/javascript;charset=utf-8" application/javascript text/javascript
"text/css;charset=utf-8" text/css
"text/plain;charset=utf-8" text/plain;
server {
listen 80;
server_name www.domain.tk domain.tk;
location /static/ {
alias /home/myuser/myapp/static/;
add_header Cache-Control public;
expires 14d;
access_log off;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/myuser/myapp/myapp.sock; # REPLACE user
}
}
}
I'm using a uWSGI server and a Flask application if that matters. The developer console says the app should leverage Browser caching, how do I configure Nginx to work with serving the static files?

Nginx Configure Static Site and Django+uWSGI

I am having a bit of trouble getting Nginx to serve a static index.html page, and also to serve a a Django site. Could anyone point me out to what I'm doing wrong?
I have port 80 and 8000 open.
I have spent three days trying to get this working.
Locally I have no problems, then again I'm not using nginx for that.
I placed uwsgi_params within /etc/nginx/uwsgi_params
/etc/nginx/sites-enabled
Here are my symlinks to the actual configuration files
site1.conf -> /home/jesse/projects/site1/conf/site1.conf
site2.conf -> /home/jesse/projects/site2/conf/site2.conf
/home/jesse/projects/site1/conf/site1.conf
This is just a basic static site, but it won't load :(
server {
listen 80;
server_name www.site1.com;
rewrite ^(.*) http://site1.com$1 permanent;
location / {
root /home/jesse/projects/site1/;
}
}
/home/jesse/projects/site2/conf/site2.conf
= The manage.py/wsgy.py is located under /home/jesse/projects/site2/site2/
= This is a Django site using uWSGI, I installed it with $ pip install uwsgi.
server {
listen 80;
server_name www.site2.com;
rewrite ^(.*) http://site2com$1 permanent;
root /home/site2/projects/site2/site2;
location /static/ {
alias /home/jesse/site2/projects/site2/site2/static/;
#expires 30d;
}
location /media/ {
alias /home/jesse/site2/projects/site2/site2/media/;
#expires 30d;
}
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
}
}
/home/site2/projects/site2/conf
[uwsgi]
projectname = site2
projectdomain = site2.com
base = /home/jesse/site2/projects/site2/site2
# Config
plugins = python
master = true
protocol = uwsgi
env = DJANGO_SETTINGS_MODULE=%(projectname).settings
pythonpath = %(base)/src/%(projectname)
module = %(projectname).wsgi
socket = 127.0.0.1:8000
logto = %(base)/logs/uwsgi.log
# Runs daemon in background
daemonize = /home/jesse/log/$(projectname).log
Nginx Restart
$ sudo service nginx restart
* Restarting nginx nginx [ OK ]
= The site1 produces a Not Found (Not a 404)
= The site2 produces a
I would appreciate any assistance :)
rewrite ^(.*) http://site1.com$1 permanent;
this in site1, is not managed by any server, because no server_name handles site.com (without www).
then again with site2
rewrite ^(.*) http://site2com$1 permanent;
first fix those lines.
the correct way in my opinion is to write a server rule that catches www. names and rewrites them to non www, and place site1.com or site2.com as server_name in the rules you have now, as an example of rewiring
server {
listen 80;
server_name www.site1.com
return 301 http://site1.com$request_uri?;
}

Categories