How to configure docker-compose.yml and nginx conf file to read an external drive? - azure

I have nginx in a docker container. My docker-compose.yml is like this (simplified):
nginx:
volumes:
- /var/www/html:/www/:rw
- /media/storage:/storage/:rw
Where /var/www/html is my website root and /media/storage is an external drive in my host machine (Azure).
Now I'm trying to point the website URL example.com/downloads to /storage but without success. My nginx/conf.d/example.com.conf is as following (simplified):
server {
listen 80 default;
server_name example.com;
# this works
root /www;
index index.php;
# this get a 404 error
location /downloads{
root /storage;
}
}
But I get a 404 error for example.com/downloads. What am I forgetting here? The file permissions and owner to both paths are the same. I don't know if the bad configuration is in example.com.conf or in docker-compose.yml. How should I configure these?

I solved this myself using alias /storage; instead of root /storage.

Related

Can't redirect traffic to localhost with nginx and docker

I'm new to Docker and nginx so this may be a simple question but I've been searching through question/answers for a while and haven't found the correct solution.
I'm trying to run an nginx server through docker to reroute all requests to my.example.com/api/... to localhost:3000/api/...
I have the following Dockerfile:
FROM nginx
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
and the nginx.conf file:
server {
server_name my.example.com;
location / {
proxy_pass http://localhost:3000/;
}
}
When I make the calls to the api on localhost:3000 this works fine but when I try to run against my.example.com I get a network error that the host isn't found. To be clear the domain I want to 'redirect' traffic from to localhost host is a valid server address, but I want to mock the api for it for development.
This isn't working because your nginx proxying request to localhost which is container itself but your app is running on host's port 3000 -- outside of container. check this article
change
proxy_pass http://localhost:3000/;
  to
proxy_pass http://host.docker.internal.
add 127.0.0.1 example.com my.example.com in etc/hosts

nginx not serving wsgi

I'm doing this tutorial to setup my django application to run on nginx. Everything works except for nginx serving the wsgi it is supposedly listening for on port 8001.
I run uwsgi with uwsgi --socket :8001 -b 32000 --wsgi-file test.py - this can be served as http on port 8000 fine as earlier in the tutorial.
Below is my .conf file, but some things I am confused about is what should be in the /etc/nginx/sites-*/ folders. Currently I have a symlink of the below .conf file in the sites-enabled directory.
# mysite_nginx.conf
# 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:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias _; # your Django project's media files - amend as required
}
location /static {
alias ~/path/i/changed/to/app/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 ~/path/i/changed/to/uwsgi_params; # the uwsgi_params file you installed
}
}
I got this to work by launching nginx with a specified config file, i.e. nginx -c /path/to/mycoolsite_nginx.conf. Still not sure what the sites-* folders are actually for...

Configure nginx for two node apps, with one on a subdomain

Issue
I'm trying to set up nginx so I can have my domain, domain.com run by a node web app on port 3000, and the subdomain dev.domain.com run by a second node web app on port 3001. When I run this configuration domain.com is connected to the right port, but dev.domain.com just gives a page that says the server can't be reached.
Edit:
If I go to IP_ADDRESS:3000 I get the same content as domain.com, but if I go to IP_ADDRESS:3001 I get what should be at dev.domain.com. Based on this it seems like the apps are running fine on the right ports, and I'm just not routing the subdomain correctly.
Code
I edited /etc/nginx/sites-available/default directly so it has:
server {
listen 80 default_server;
server_name domain domain.com www.domain.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
server {
listen 80;
server_name dev.domain dev.domain.com www.dev.domain.com;
location / {
proxy_pass http://127.0.0.1:3001;
}
}
Other than that file everything else is a fresh install
My logic
I'm very new to nginx but this seems like any requests for domain.com would get sent to port 3000, and requests for dev.domain.com would go to 3001.
Any help or critique of what I've done so far would be greatly appreciated!
Above setup works fine. My issue was with DNS records - I added an A record directing dev.domain.com to the IP address of the server I'm running the node apps on.
Faced same issue and solved it by creating file from root user:
drwxr-xr-x 6 gitlab-runner gitlab-runner 4096 Sep 12 06:56 .
drwxr-xr-x 4 root root 4096 Sep 12 06:57 ..
-rw-r--r-- 1 root root 11 Sep 12 06:54 .env
-rw-rw-r-- 1 gitlab-runner gitlab-runner 599 Sep 12 06:56 app.js
If you will delete all files and directories in this folder from gitlab-runner with rm -Rf command it will delete all files except .env
This is just quick workaround may be will be useful.

Nginx + node.js configuration

I need the right configuration of nginx for my problem.
Suppose the nginx + nodejs serverprograms are running on the same debian machine.
Domain name for my website is for simplicity just webserver.com (and www.webserver.com as alias)
Now, when someone surfs on the internet to "webserver.com/" it should pass the request to the nodejs application which should run on a specific port like 3000 for example. But the images and css files should get served by nginx as static files and the filestructure should looke like webserver.com/images or webserver.com/css .. images + css should get served by nginx like a static server
Now it gets tricky:
But when someone surfs on webserver.com/staticsite001 or webserver.com/staticsite002 then it should get served by the nginx server only. no need for nodejs then.
And for the nodejs server, I am just setting up my nodejs application with port 3000 for example to receive the bypass from nginx for webserver.com/
to put it in a more understandable language: when someone surfs to webserver.com/staticsite001 it should NOT pass it to the node application. It should only pass it to the node application if its inside of the first webserver.com/ directory that the outsiders can see. The webserver.com/staticsite001 should only get serverd by nginx.
How, how do I do that ? And what should the http and server block look like for the nginx configuration look like?
I am familiar with nodejs. But I am new to nginx and new to reverse proxying.
thanks
the file structure on the debian hard drive looks like:
/home/wwwexample/staticsite001 (for www.webserver.com/staticsite001/) only handled by nginx
/home/wwwexample/staticsite002 (for www.webserver.com/staticiste002/) only handlex by nginx
/home/wwwexample/images
/home/wwwexample/css
and in
/home/nodeapplication is my node js application
This server block should work:
server {
listen 80;
server_name webserver.com www.webserver.com;
root /home/wwwexample;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
}
location /staticsite001 {
}
location /staticsite002 {
}
location /images {
}
location /css {
}
}
First location makes nginx to proxy everything to localhost:3000. Following empty locations instruct nginx to use default behavior, that is to serve static files.
Put this code into file /etc/nginx/sites-available/my-server and create a symlink to it in /etc/nginx/sites-enabled. There is a default config, which you could use as a reference.
After that you could use command sudo /usr/sbin/nginx -t to check configuration. If everything is OK use /etc/init.d/nginx reload to apply new configuration.

configure nginx to get js and css directly from public folder of node.js express site

I got node.js express site on port 3000. And it is configured to work with nginx.
It worked well through nginx until I decided to configure nginx to get css, js and image files directly from public folder without node.js express:
/etc/nginx/sites-enabled# cat myDomain.com.public
server {
listen 80;
server_name myDomain.com;
access_log /var/log/nginx/myDomain.com.access.log;
location / {
proxy_pass http://127.0.0.1:3000/;
}
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /root/pathToNodeJsExpressSiteFolder/public;
access_log off;
expires max;
}
}
But now this site works without css and js. It returns 403 forbidden requesting them:
style.css
/stylesheets
GET
403
Forbidden
What I did wrong?
it requires both read and execute privilege to the directory and all parent directories of this directory for those static resources on file system for nginx. If you found some entries in your nginx error log like:
open() "/root/pathToNodeJsExpressSiteFolder/public/stylesheets/style.css" failed (13: Permission denied)
Then you need:
$ sudo chmod +rx /root/pathToNodeJsExpressSiteFolder/public
$ sudo chmod +rx /root/pathToNodeJsExpressSiteFolder
$ sudo chmod +rx /root
Note: $ sudo chmod +rx /root is not recommended because it always limits non-root user to access /root.
A 403 forbidden response may be related to the nginx process don't having permission to read the files. Check that the folder public is recursively readable by the user running nginx and change the permissions if needed:
sudo chmod -R 744 /root/pathToNodeJsExpressSiteFolder/public

Resources