Nginx+Gunicorn - reverse proxy not working - nginx

I am trying to setup a python flask application on a server following this guide: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04. I have this working running on my local machine by following the guide. However when I am trying to implement on the actual server with the same config I am running into an issue on proxying requests back to the gunicorn server. I am able to serve static content from Nginx with no problem. When I make a web service call from the static content back to Nginx, it should be proxied back to the gunicorn server.
For example when I try to make the call 'http://example.com/rest/webService', I would expect Nginx to pass anything starting with /rest/ back to gunicorn. The error below is all I can see in the error logs about what is happening:
2019/01/18 12:48:18 [error] 2930#2930: *18 open() "/var/www/html/rest/webService" failed (2: No such file or directory), client: ip_address, server: example.com, request: "GET /rest/webService HTTP/1.1", host: "example.com", referrer: "http://example.com/"
Here is the setup for python_app:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html;
location ^/rest/(.*)$ {
include proxy_params;
proxy_pass http://unix:/home/username/python_app/python_app.sock;
} }
The only change to my nginx.conf file was to change 'include /etc/nginx/sites-enabled/*' to 'include /etc/nginx/sites-enabled/python_app'.
Please let me know if you have any ideas at all on what I may be missing! Thanks!

Not a solution, but some questions....
If you run
sudo systemctl status myproject
Do you see affirmation that gunicorn is running, and what socket it is bound to?
And does
sudo nginx -t
come back saying no diagnostic?
The regex in the location block for nginx -- I don't see anything similar to that in the guide, I see that you're trying to capture everything after "rest/", but looking at the nginx documents, I think you'd have to have $1 to reference the captured part of the URL. Can you try without the "^/rest/(.*)$" and see whether nginx finds anything?
Is the group that owns your directory a group that nginx is part of (a lot of setups are www-data)

Related

Nginx list contents of network share via UNC?

I've seen a couple other questions that talk about listing contents of a directory by using autoindex on. I'm trying to use this in conjunction with a network share and it is not working.
server {
listen 80;
server_name downloads.example.com;
root c:/webroot/downloads/;
location / {
index index.html;
}
location /drivers/ {
alias //10.1.0.20/drivers;
autoindex on;
}
}
I can access downloads.example.com and get the static index as expected, but when I try to access downloads.example.com/drivers/ I get a 500 Internal Server Error. My Nginx error log shows the following entry:
2019/09/20 07:57:10 [crit] 4760#2720: *4 GetFileAttributesEx()
"//10.1.0.20/drivers" failed (58: The specified server cannot perform
the requested operation), client: 10.1.0.1, server:
downloads.example.com, request: "GET /drivers/ HTTP/1.1", host:
"downloads.example.com", referrer: "http://downloads.example.com/"
I can access \\10.1.0.20\drivers from this machine via File Explorer without issues.
Why isn't this working?
I was able to get this to work by creating a symbolic link instead of using UNC directly from NGinx.
mklink -d c:\drivers \\10.1.0.21\drivers
Then use c:\drivers as the alias in the config.

nginx proxy_pass to flask works while /static fail to find jquery

I'm a noob an nginx (and apache and php ...)
I've this flask app that works fine from:
http://127.0.0.1:5000
and also externally from
http://myhost.com:5000
I would like to use that from
myhost.com/rest_1
to make room to others rest, like myhost.com/rest_2 .. 3 and so on.
The app resides in
/opt/rest_1
and uses some resources from it's /static folder like css and jquery.
I've this
/etc/nginx/sites-available/rest_1.conf
server {
listen 80;
listen [::]:80;
server_name myhost.com;
server_name_in_redirect off;
location /rest_1 {
rewrite ^/rest_1(.*) /$1 break;
proxy_pass http://127.0.0.1:5000/;
}}
and it's link to sites-enabled.
nginx restart and reload with no errors.
Other configurations are default from installation.
When i try to connect to
myhost.com/rest_1
I can see an incomplete page partially working, looking at nginx log:
/var/log/nginx/error.log:
*8 open() "/usr/share/nginx/html/static/w3.css" failed (2: No such file or directory), client: xx.xx.xx.xx, server: myhost.com, request: "GET /static/w3.css HTTP/1.1", host: "myhost.com", referrer: "http://myhost.com/rest_1"
So, it is clear to me that '/usr/share/nginx/html/' is got from elsewhere... and should also have a proper name that piece of folder (I'm a noob!!)
How to tell to the engine to redirect to the correct path on
/opt/rest_1
to get back all /static functionalities ?
OS: ubuntu server 16.04
nginx: 1.10.3
Thanks.
Cause you don´t set one location resource for your static content. Your app is referencing an CSS from de root folder and not from rest_1.
Your CSS Call was http://myhost.com/STATIC, so it not match in the location that you set and try inside the default.
So you can solve it in your app or you can set it in your nginx as below:
location /static {
root /opt/rest_1;
}

unable to configure and start nginx

my nginx server seemed to run fine but when i do netstat -tupln, I cant see it bound to port 80.
When I fire a http request, it gives me
502 Bad Gateway
---
nginx/1.4.6 (Ubuntu)
Following is the nginx config I have written to both
/etc/nginx/sites-available/mysite.conf
and /etc/nginx/sites-enabled/mysite.conf
server {
listen 80;
server_name _;
location ~ / {
proxy_pass http://127.0.0.1:8001;
}
}
I am able to run following commands without any error.
nginx start/stop/restart
but making a http request to the machine gives me following error in /var/log/nginx/error.log
08:39:26 [warn] 17294#0: conflicting server name "_" on 0.0.0.0:80, ignored
08:41:17 [error] 20186#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.123.123.123, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "123.123.123.123"
Even changing the port 8001 to 8003 in mysite.conf files in /etc/nginx/sites-* and restarting nginx doesn't make any difference in above error message which makes me believe that it isn't picking up changes in the conf files.
Can anybody help me understand what is it that i am missing?
It is an old issue. Would like to put my finding here in case someone encounters the same issue in the future. The way I resolve this issue is by changing /etc/nginx/ permission.
sudo chmod -R 777 /etc/nginx/
Probably more than necessary, but that resolve my problem. Please let me know if anyone find any solid solution

uWSGI nginx error : connect() failed (111: Connection refused) while connecting to upstream

I'm experiencing 502 gateway errors when accessing my IP on nginx(http://52.xx.xx.xx/), the logs simply says this:
2015/09/18 13:03:37 [error] 32636#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: xx.xx.xx.xx, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8000", host: "xx.xx.xx.xx"
my nginx.conf file
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name xx.xx.xx.xx; # substitute your machine's IP address or FQDN
charset utf-8;
access_log /home/ubuntu/test_django/nginx_access.log;
error_log /home/ubuntu/test_django/nginx_error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/test_django/static/media/; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/test_django/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/test_django/uwsgi_params; # the uwsgi_params file you installed
}
}
Is there anything wrong with nginx.conf file.....if i use default conf then it is working.
I resolved it by changing the socket configuration in uwsgi.ini
from socket = 127.0.0.1:3031, to socket = :3031. I was facing this issue when I ran nginx in one Docker container and uWSGI in another. If you are using command line to start uWSGI then do uwsgi --socket :3031.
Hope this helps someone stuck with the same issue, during deployment of a Django application using Docker.
change this address:
include /home/ubuntu/test_django/uwsgi_params;
to
include /etc/nginx/uwsgi_params;
I ran into this issue when setting up the env by nginx + gunicorn and solve it by
adding '*' to ALLOWED_HOSTS or your specific domain.
In my case with a debian server it worked moving:
include /etc/nginx/uwsgi_params;
In the location tag in my nginx server config file, like this:
location /sistema {
include /etc/nginx/uwsgi_params;
uwsgi_pass unix://path/sistema.sock;
}
Also, check you have the following packages installed:
uwsgi-plugin-python
pip3 install uWSGI did the trick for me :D

Nginx and upstream : configuration failed

I try to test load balancer with nginx so I add upstream like the documentation exemple. But when I try to reload nginx to refresh the configuration, the refresh fail.
I just added upstream bloc, if I remove it all work
file : /etc/nginx/sites-available/default
upstream backend {
server webserver1:80;
server webserver2:80;
}
server {
listen 80;
listen [::]:80;
server_name www.interceptlocalcall.io interceptlocalcall.io;
location /users {
proxy_pass http://127.0.0.10:18000;
}
...
when I reload with the upstream block :
[FAIL] Reloading nginx configuration: nginx failed!
Nginx version :
nginx version: nginx/1.6.2
I see anything about this error, so I think I just miss something, perhaps in the install ?
If you have any idea thanks.
Please test your configuration by running $ nginx -c /etc/nginx/nginx.conf -t — it will tell you more detailed information about what's wrong with your configuration.
These messages also written to global error.log file (not one you define for servers but one defined in http section), usually located in /var/log/nginx/error.log.

Resources