How to place GitLab behind nginx - nginx

I just got a Raspberry Pi 4B and i would like to place on it Nextcloud and Gitlab servers.
Using the instructions i was able to install GitLab successfully on my RPI4.
As second step I diceded to install Nginx, since i would like to run more then one server on RPI4.
I followed the instructions and added gitlab-omnibus-nginx.conf to my
/etc/nginx/modules-enabled/
as I tried to restart my nginx i got using sudo nginx -t an error:
nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/modules-enabled/gitlab-omnibus-nginx.conf:19
nginx: configuration file /etc/nginx/nginx.conf test failed
I belived that the problem is not there, since if i commit this line, i get
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/modules-enabled/gitlab-omnibus-nginx.conf:23
nginx: configuration file /etc/nginx/nginx.conf test failed
My /etc/nginx/nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

It looks the sample from /etc/nginx/modules-enabled/gitlab-omnibus-nginx.conf is not a valid nginx config file, but just a snippet instead. The upstream and the server blocks need to be nested within the http block of your nginx config file.

Related

Static location with nginx downloading file instead of showing response

I've read about this problem with php a numerous amount of times by now. I'm trying to get the basic of nginx since I almost exclusively used apache or iis.
I'm running a small debian 9 server and trying to figure out the basics of the nginx config.
the nginx.conf file is mostely untouched by me.
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
The default.conf file however is edited by me:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 10.20.30.1;
root /var/www;
index index.html index.htm index.nginx-debian.html;
}
when accessing via IP, I get everything from my /var/www folder correctly loaded up with css files. So the including mime-type is working quite well.
after adding:
location /greet {
return 200 "Hello User!";
}
to the server configuration I'm able to access http://10.20.30.1/greet but instead of displaying the message within the browser it's being downloaded as a file without extension, called greet with my message inside.
I found many simular problems like mine regarding php. In this case php shouldn't even be an issue by now.
Any advice would be very helpful.
thank you in advance!
That return statement literally sends a text response with the default content type. If the browser does not understand how to present a given content type, it will offer to download the file.
You can tell the browser that it's plain text using the default_type directive.
For example:
location /greet {
default_type text/plain;
return 200 "Hello User!";
}

I don't understand how nginx is finding gunicorn in my setup

Largely following the instructions at https://tutos.readthedocs.io/en/latest/source/ndg.html, I got an nginx-gunicorn-django stack running, and it's been running reliably for weeks now. While looking at how to add something (user-uploaded media files) I looked into my nginx configuration, and it looks like I haven't edited any mention of gunicorn into it. I'd like to understand what's going on before I make any more complex changes. How can nginx be forwarding requests to gunicorn, when there are no mentions of gunicorn anywhere in the nginx configuration directory tree?
My main nginx configuration file looks like this, which I think is the default from the installation:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
and my gunicorn start script is:
#!/bin/bash
# Based on an example from https://tutos.readthedocs.io/en/latest/source/ndg.html
NAME="makers" # Name of the application (*)
DJANGODIR=/var/www/makers # Django project directory (*)
SOCKFILE=/var/www/makers/run/gunicorn.sock # we will communicate using this unix socket (*)
USER=nginx # the user to run as (*)
GROUP=webdata # the group to run as (*)
NUM_WORKERS=1 # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=makers.settings # which settings file should Django use (*)
DJANGO_WSGI_MODULE=makers.wsgi # WSGI module name (*)
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /var/www/makers_venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /var/www/makers_venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user $USER \
--bind 0.0.0.0:8000
# --bind=unix:$SOCKFILE
How nginx pass the request to WSGI server (i.e. gunicorn) is defined in one of the location directive within the server block, which is included by the http block that you can see in your /etc/nginx.conf file:
http {
# other http settings
.....
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Within the file where the server is defined, among all the server and location directives you will likely see the setting like this:
server {
...
location / {
...
# if your guincorn is listening to an IP add
proxy_pass http:127.0.0.1:8000
# or if your gunicorn is listening to a unix socket
# proxy_pass http://unix:/var/www/makers/run/gunicorn.sock
}
Based on your gunicorn start script, your nginx is passing the request to gunicorn via an IP address at port 8000:
exec /var/www/makers_venv/bin/gunicorn makers.wsgi:application --bind 0.0.0.0:8000
I would recommend you read Understanding the Nginx Configuration File Structure and Configuration Contexts, or my blog for better understand on how nginx and gunicorn(or WSGI in general) works.

Nginx Not Showing Static Content on Localhost

I'm working on creating a basic Nginx server to show a static HTML webpage (for now) and I am having an issue viewing my content. I've followed the tutorial here, by creating a new server block, named quake.dev. I have removed the symlink to the default server block in /etc/sites-enabled/default and created the symlink between sites-enabled and sites-available for quake.dev
My nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
quake.dev
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/quake.dev/html/;
# Add index.php to the list if you are using PHP
index index.html index.htm;
server_name quake.dev www.quake.dev;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
allow 192.168.0.1/24;
allow 127.0.0.1;
deny all;
}
}
I then added quake.dev to my /etc/hosts file:
hosts
#127.0.0.1 localhost
127.0.0.1 quake.dev
127.0.1.1 wintermute
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
After restarting both nginx and the networking service, I load quake.dev into Chrome and it returns ERR_CONNECTION_REFUSED. Is there anything anyone can think of? I've been up and down this issue for days now.
UPDATE: turned out there was a broken symlink between sites-enabled and sites-available. places dunce cap on head

why does nginx fail to start?

Yet starting nginx fails and my log shows:
[emerg] 55#55: "server" directive is not allowed here in /etc/nginx/nginx.conf:1
What am I doing wrong? Not sure if it matters but this is inside a docker container.
Try debugging first with
sudo nginx -t
If this passes but nginx restart fails, try checking
your nginx logs to narrow down the bug
/var/log/nginx/error.log
/etc/nginx/nginx.conf is not the place to store your server setups. This file should contain information on the user it will run under and other global settings. Typically it will contain stuff like this:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 500M;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Where you should put your individual server setups, is in /etc/nginx/sites-enabled/. As you can see, all files inside that folder are being included in the http block of your nginx.conf file.
So store the sample config you have posted inside /etc/nginx/sites-enabled/some-random-name.conf, and restore the nginx.conf file to the config I posted above, for instance, and you should be good to go.

vhost and dns with nginx

i setup a virtualbox with a clean install of ubuntu 16. installed nginx, php7, mysql all fine. the computer name is: mercury
i've setup a folder that would be the root for all my web projects: /var/www
i want to be able to have a dynamic virtual host where i can just create a folder (like: /var/www/project1) and i'll easily be able to access it via the browser at: project1.mercury
i can i achieve this? in my browser: mercury/ loads up fine (/var/www/index.html), but as soon as I use a subdomain it craps out and gives me a dns error: server DNS address could not be found
here is my: /etc/nginx/nginx.conf file
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
worker_rlimit_nofile 30000;
events { worker_connections 1024; }
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
gzip on;
gzip_disable "msie6";
gzip_types text/plain text/xml text/css text/comma-separated-values;
upstream app_server {
server 127.0.0.1:8080 fail_timeout=0;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-enabled/default:
server {
listen 80;
server_name ~^(?P<subdomain>.+)\.mercury$;
location / {
root /var/www/$subdomain;
}
}
UPDATE:
so this config works, but i need to manually update my Windows hosts file and specify the subdomain with the same ip address:
192.168.1.101 project1.mercury
if i dont, i get a dns error.
how can this be achieved without having to manually add an entry in the hosts file everytime??

Resources