LeaYeh / webserver

MIT License
3 stars 0 forks source link

[DEVOPS] Setup Nginx server as golden result and branchmark #48

Open LeaYeh opened 5 days ago

LeaYeh commented 5 days ago

Start Nginx server and check our beautiful homepage and template 🥂🥂🥂 http://127.0.0.1:8080/

image

This one is quite important, before we start to implement each submodule we need to compare our understanding with Nginx behavior.

Nginx usage:

  1. Prepare Nginx
    • Use devcontainer, I already setup everything in it (Recommanded)
    • Install Nginx on your local machine
  2. Check nginx config
    • I prepared a sample in file: tests/data/configs/nginx_reference.conf
    • Need to able to config for your own function
  3. Start Nginx with correspond config file

testing config syntax

> nginx -t -c /workspace/tests/data/configs/nginx_reference.conf

// console output
nginx: the configuration file /workspace/tests/data/configs/nginx_reference.conf syntax is ok
nginx: configuration file /workspace/tests/data/configs/nginx_reference.conf test is successful

Start Nginx server with config file

> nginx -c /workspace/tests/data/configs/nginx_reference.conf
image

Stop Nginx server

> nginx -s stop                                              

// console output
2024/09/13 05:17:41 [notice] 8629#8629: signal process started
LeaYeh commented 5 days ago

What I learned from Nginx:

  1. how important the content-type is
    • I spent almost 2h to debug why the index.html did not apply CSS, and I realized if the content-type is wrong the browser can not render correctly
  2. If the *.html include others resource then the browser will sent a new request for it
    • e.g. <link rel="stylesheet" href="/css/styles.css">
  3. How to config correctly

    location / {
            root /workspace/www/html;
            index index.html;
        }
    
        # This block is used to define the location of the css directory
        location /css/ {
            root /workspace/www;
            index styles.css;
            default_type text/css;
        }
LeaYeh commented 5 days ago

This PR might raise issue #49