Installing & Using Nginx HTTP (and Reverse Proxy) Server in Ubuntu LTS

Installing & Using Nginx HTTP (and Reverse Proxy) Server in Ubuntu LTS

2012-04-24. Category & Tags: HTTP, Server, Reverse Proxy

nginx is a great alternative to apache2, it is better to handle static web pages and files. another one is lighttpd which is optimized for long-time keep-alive (a lot of connections).

Install in Ubuntu #

18.04 ~ 22.04 LTS #

sudo apt install -y software-properties-common && sudo add-apt-repository ppa:nginx/stable

sudo apt update && \
sudo apt install -y nginx nginx-extras && \
systemctl status nginx

ref: digitalocean

10.04 LTS #

sudo -s
apt-get install gcc g++;
wget http://downloads.sourceforge.net/pcre/pcre-8.10.tar.bz2 (seems hard-coded version in nginx v1.2; otherwise, such as me, ln -s xxx.so.xxx ...); tar xvjf ...; cd ...; ./configure; make; make install;
cd nginx; ./configure --without-http_gzip_module (do not need and do not want in experiments); make; make install;
/usr/local/nginx/conf# vim nginx.conf; keepalive_timeout  0\. #server_name non-local???

the second time when i started it, it could start work, not the first time. do not know why. try nginx; nginx -s stop; nginx.

ref: asep.us

run & check #

sudo service nginx restart && \
service nginx status && \
tail -f /var/log/nginx/access.log

# dated:
sudo systemctl restart nginx.service && \
systemctl status nginx.service && \
tail -f /var/log/nginx/access.log

Config (Normal Web) #

sudo vi /etc/nginx/sites-enabled/my_site_nick_name

minimal config #

server {
    listen 80;
    server_name mysite.com;
    root /path/to/site/public/dir;
    index index.html index.htm index.nginx-debian.html;
    location / {
        try_files $uri $uri/ =404;
    }
}

WARN: do NOT put “root” inside “location”, which may cause issues.

ref

a little more (suggested for init) #

server {
    listen 80;
    listen [::]:80;

    server_name my_site.com;
    root /<path/to/site>/dir;

    # Add index.php when using PHP
    index index.html index.htm index.nginx-debian.html;

    location / {
        # First attempt to serve request as file, then as directory, then fall back to 404.
        try_files $uri $uri/ =404;
    }

    error_page 404 500 502 503 504 =200 /404.html; # recode the 404 etc. as 200 and serve the customized content.
    location = /404.html {
        root /<path/to/site>/dir;
        internal; # allow only nginx to access, canNOT access directly from browsers.
    }
}

more security config #

nginx.conf: #

sudo vi /etc/nginx/nginx.conf :

...
http{
  error_page 401 403 404 405 406 409 500 502 503 504 =200 index.html;
  server_tokens off; # removes versions of Nginx and OS
  more_clear_headers Server; # removes the Server header completely. requires apt install nginx-extras
  more_set_headers 'Server: Windows10'; # set server info. requires apt install nginx-extras
  ...
}

default_server: #

set the home (index) page for “default_server” (/etc/nginx/sites-enabled/default):

sudo cp redirect-google.html /var/www/html/index.html
sudo cp redirect-google.html /var/www/html/index.htm

redirect-google.html with the content:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="refresh" content="7; url='https://www.google.com'" />
    <script>
      location.replace("https://www.google.com");
    </script>
  </head>
  <body>
    <p><a href="https://www.google.com">Google</a></p>
  </body>
</html>

debug #

Problem: “403 forbidden” for sites out of /var/www/
Reason: folder x permission.
Solution: use namei -om <folder> or ls -al to check all folders through the path gives x permission to nginx (other), otherwise chmod o+x (e.g. chmod o+x ~/Dropbox).

Config (Reverse Proxy) #