[Linux] Setting Up Nginx Block
Tags: backend, nginx
Published: 2023-05-17 07:25:35

**What is nginx block ?**

- is a config file on how to serve your app. It is part of Nginx webserver settings file. They are located at `/etc/nginx/sites-available/`
- Same as Apache which is another webserver, it also has its own config 'block' to serve your app.

---
**Example a Standard Block for PHP project**

```
server {
    # listen to port 80 for IPv4
    listen 80;
    # listen to port 80 for IPv6
    listen [::]:80;
    # the address domain of your app 
    server_name dev.mydomain.com;`

    # location of your SSL certificates if in use
    #ssl_certificate /ssl/crt/file.crt;
    #ssl_certificate_key /ssl/key/file.key;

    # location of 'myproject' folder where index.php located
    root /var/www/development/myproject/public;
    # name of index file entry point
    index index.php index.html index.htm index.nginx-debian.html;

    # add additional header before request reach your app
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    # optional condition to block request coming from other address
    if ($host != "dev.mydomain.com") {
       return 404;
    }

    # Optional, prevent Direct Access To Protected Files
    location ~ \.(env|log) {
        deny all;
    }

    # Optional, prevent Direct Access To Protected Folders
    location ~ ^/(^app$|bootstrap|config|database|overrides|resources|routes|storage|tests|artisan) {
        deny all;
    }

    # Optional, prevent Direct Access To modules/vendor Folders Except Assets
    location ~ ^/(modules|vendor)\/(.*)\.((?!ico|gif|jpg|jpeg|png|js\b|css|less|sass|font|woff|woff2|eot|ttf|svg|xls|xlsx).)*$ {
        deny all;
    }

    # passing address parameters from request
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    # Pass PHP Scripts To FastCGI Server
    location ~ \.php$ {
        # passing address parameters from request
        try_files $uri $uri/ /index.php?$query_string;

        # set version of php fpm to serve the php project
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny all other requests that not match
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

---
**Example a Standard Block for PHP project with multiple subdomains**

```
server {
    listen 80;
    listen [::]:80;
    server_name dev.mydomain.my dev2.mydomain.my;

    root /var/www/production/myproject/public;
    index index.php index.html index.htm index.nginx-debian.html;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location ~ \.php$ {
        try_files $uri $uri/ /index.php?$query_string;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

---
**Example a Standard Block for Reverse-Proxy to Port**

- this is usually use to serve Node projects

```
server {
    listen 80;
    listen [::]:80;
    server_name dev.mydomain.com;

    if ($host != "dev.mydomain.com") {
       return 404;
    }

    location / {
       # example proxy-ing port 80 to port 1337 in server localhost
       proxy_pass      http://localhost:1337;
       
       # set up headers towards proxy
       proxy_set_header    Host             $host;
       proxy_set_header    X-Real-IP        $remote_addr;
       proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
       proxy_set_header    X-Client-Verify  SUCCESS;
       proxy_set_header    X-Client-DN      $ssl_client_s_dn;
       proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
       proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
       proxy_read_timeout 1800;
       proxy_connect_timeout 1800;
       chunked_transfer_encoding on;
       proxy_set_header X-NginX-Proxy true;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
       proxy_http_version 1.1;
       proxy_redirect off;
       proxy_buffering off;
    }
}
```