Nginx list contents of network share via UNC? - nginx

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.

Related

Nginx+Gunicorn - reverse proxy not working

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)

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

Nginx working with apache-like locations

I am trying to migrate my personal sterver from apache to nginx, but i cant get the location to work.
My server have several apps, avaliable in some /something from root, like url.com/git url.com/mediaWiki, url.com/vnstat url.com/redmine
In apache a have a bunch of config files for each app:
redmine.conf
<Location "/redmine">
Options none
Require all granted
PassengerEnabled On
RailsBaseURI /redmine
RailsEnv production
</Location>
vnstat_php.conf
Alias /vnstat /var/www/vnstat_php
<Directory /var/www/vnstat_php>
DirectoryIndex index.php
Require all granted
</Directory>
I am trying to replicate this on nginx, but my best try so far end up with a weird url. I only manage the thing to work writing on the main ngix config file:
server {
listen 8123 default_server;
listen [::]:8123 default_server;
server_name _;
root /var/www/html;
location / {
}
location /vnstat/ {
root /var/www/vnstat_php/;
index index.php;
}
}
The root page is working ok, but the /vnstat link send me to
2017/02/20 11:37:19 [error] 27538#0: *1 "/var/www/vnstat_php/vnstat/index.php" is not found (2: No such file or directory), client: 177.92.59.216, server: _, request: "GET /vnstat/ HTTP/1.1", host: "url.com:8123"
It is looking for a vnstat directory inside /var/www/vnstat_php/ instead using it as root. Can anyone point me in the right direction?
As described at http://nginx.org/en/docs/http/ngx_http_core_module.html#root
A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.
you should use alias for this case:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
location /vnstat/ {
alias /var/www/vnstat_php/;
}
But using PHP with nginx you should consider to work with FastCGI: https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/

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 502 when serving error page content?

I've been setting up Nginx as a reverse proxy for an app on the server. Part of this includes a maintenance page that has external content (like images). I was able to find a method for setting up an error page with images returning 200, but it looks like a reverse proxy will change the whole environment. Here's the original solution from nginx maintenance page with content issue
error_page 503 #maintenance;
location #maintenance {
root /path_to_static_root;
if (!-f $request_filename) {
rewrite ^(.*)$ /rest_of_path/maintenance.html break;
}
return 200;
}
The Reverse Proxy is configured as:
location / {
proxy_pass http://127.0.0.1:9007/;
proxy_redirect off;
}
The Problem is that when a file is found to exist in the "maintenance" root, something goes wrong and the server returns a 502. Anyone know what the cause could be?
Some speculation I'm wondering if server listens on port 80, it somehow passes any good file request back into the proxy. If that were true, how would that be avoided?
Edit
Here's the error in the nginx log. I am directly trying to access 50x.html. Not sure why this would occur?
2012/02/17 19:39:15 [error] 21394#0: *13 connect() failed (111: Connection refused) while connecting to upstream, client: (my ip address), server: _, request: "GET /50x.html HTTP/1.1", upstream: "http://127.0.0.1:9007/50x.html", host: "domain.com"
It looks like it is indeed trying to GET from the app and not the root. How can I bypass this?
Edit 2
I originally thought I had found an answer where a change was made for nginx v1.0.12 but it did not solve the problem. It involved a similar situation but my guess is the fix was too specific.
You shouldn't need to involve the backend (I.E., shouldn't use proxy pass) since your maintenance page should be a static html file that Nginx can serve directly.
Assuming you have a setup configured as ...
server {
listen 80;
server_name example.com;
root /path/to/webroot;
# Regular locations etc
...
}
Create a folder called "503_status" and put your maintenance page in there as "503.html".
With that in place, create a file called "maintenance.default" under the Nginx directory with the following content ...
error_page 503 /503_status/503.html;
# Candidate for redirection if not ending with one of these extensions.
if ( $request_uri !~ \.(jpg|gif|png|css|js)$ ) {
set $maint "Tr";
}
# Candidate for redirection if not a request for the maintenance page
if ( $request_uri !~ ^/maintenance/$ ) {
set $maint "${maint}ue";
}
# Redirect to a location where the status code will be issued
if ( $maint = True ) {
rewrite ^ /maintenance/ redirect;
}
# Due to Nginx quirk, issue the status code in a location context.
# Make "internal" to prevent direct browsing.
location /maintenance {
internal;
return 503;
}
# 503_status folder as "internal" so no direct browsing
location 503_status {
internal;
alias /server/path/to/503_status/folder;
}
Whenever you put to put the site into maintenance, just include the file as follows ...
server {
listen 80;
server_name example.com;
root /path/to/webroot;
include /server/path/to/maintenance.default;
# Regular locations etc
...
}
This will serve your maintenance page as well as any resources it needs (just make sure extension is in the list). The backend server does not come into play at all.

Resources