Services/Nginx: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 4: Line 4:
|-
|-
| List directory ||
| List directory ||
<source lang="nginx">
<syntaxhighlight lang="nginx">
autoindex on;
autoindex on;
</source>
</syntaxhighlight>
|-
|-
| Catch all server ||
| Catch all server ||
<source lang="nginx">
<syntaxhighlight lang="nginx">
server_name _;
server_name _;
</source>
</syntaxhighlight>
|-
|-
| Prevent 413 Too Large ... ||
| Prevent 413 Too Large ... ||
<source lang="nginx">
<syntaxhighlight lang="nginx">
client_max_body_size 128m;
client_max_body_size 128m;
</source>
</syntaxhighlight>
|}
|}


Line 24: Line 24:
=== Install ===
=== Install ===


<source lang="bash">
<syntaxhighlight lang="bash">
# Install
# Install
sudo apt install nginx
sudo apt install nginx
Line 37: Line 37:
cd /etc/nginx/sites-available
cd /etc/nginx/sites-available
sudo vim my-vhost
sudo vim my-vhost
</source>
</syntaxhighlight>


=== Setup virtual host proxy ===
=== Setup virtual host proxy ===


<source lang="text">
<syntaxhighlight lang="text">
server {
server {
   server_name my-vhost.xxx.com;
   server_name my-vhost.xxx.com;
Line 54: Line 54:
   }
   }
}
}
</source>
</syntaxhighlight>


<source lang="bash">
<syntaxhighlight lang="bash">
cd /etc/nginx/sites-enabled
cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/my-vhost my-vhost
sudo ln -s /etc/nginx/sites-available/my-vhost my-vhost
Line 65: Line 65:
# check permission of static file
# check permission of static file
sudo -u www-data ls -l /home/myaccount/vhost/my-vhost
sudo -u www-data ls -l /home/myaccount/vhost/my-vhost
</source>
</syntaxhighlight>
 
=== Add self-signed certificate ===
 
<syntaxhighlight lang="bash">
cd ~
openssl req -new > cert.csr
openssl rsa -in privkey.pem -out key.pem
openssl x509 -in cert.csr -out cert.pem -req -signkey key.pem -days 1001
cat key.pem >> cert.pem
cp cert.pem /etc/nginx/sites-available
</syntaxhighlight>
 
<syntaxhighlight lang="text">
server {
  server_name my-vhost.xxx.com;
  root        /home/myaccount/vhost/my-vhost
  access_log  /var/log/nginx/my-vhost.access.log;
  error_log  /var/log/nginx/my-vhost.error.log;
  ssl_certificate    /etc/nginx/sites-available/cert.pem;
  ssl_certificate_key /etc/nginx/sites-available/cert.pem;
 
  location / {
    proxy_pass http://192.168.25.90:12345;
    proxy_read_timeout 60;
    proxy_connect_timeout 10;
  }
}
</syntaxhighlight>


=== Certbot 2.x (Let's Encrypt) ===
=== Certbot 2.x (Let's Encrypt) ===
Line 71: Line 99:
* [https://certbot.eff.org/ Certbot official site]
* [https://certbot.eff.org/ Certbot official site]


<source lang="bash">
<syntaxhighlight lang="bash">
sudo snap install --classic certbot
sudo snap install --classic certbot
# sudo ln -s /snap/bin/certbot /usr/bin/certbot
# sudo ln -s /snap/bin/certbot /usr/bin/certbot
</source>
</syntaxhighlight>


== Install nginx on macOS 10.13+ ==
== Install nginx on macOS 10.13+ ==
Line 81: Line 109:
Tap 3rd party package is useful for development.
Tap 3rd party package is useful for development.


<source lang="bash">
<syntaxhighlight lang="bash">
brew tap denji/nginx
brew tap denji/nginx


Line 91: Line 119:
   --with-upload-module \
   --with-upload-module \
   --with-upload-progress-module
   --with-upload-progress-module
</source>
</syntaxhighlight>


Running nginx as nobody is inconvenient for development.
Running nginx as nobody is inconvenient for development.
Line 97: Line 125:


'''/usr/local/etc/nginx/nginx.conf:2'''
'''/usr/local/etc/nginx/nginx.conf:2'''
<source lang="bash">
<syntaxhighlight lang="bash">
#    user      group
#    user      group
user  myaccount admin;
user  myaccount admin;
</source>
</syntaxhighlight>


When using nobody, response would be 403 forbidden and hard to debug.
When using nobody, response would be 403 forbidden and hard to debug.
Line 106: Line 134:
Like this: ('''/usr/local/var/log/nginx/error.log''')
Like this: ('''/usr/local/var/log/nginx/error.log''')


<source lang="text">
<syntaxhighlight lang="text">
2019/02/18 15:42:47 [crit] 25478#0: *78 stat() "/Users/myaccount/Documents/0x01.Source/myvhost/public" failed (13: Permission denied)
2019/02/18 15:42:47 [crit] 25478#0: *78 stat() "/Users/myaccount/Documents/0x01.Source/myvhost/public" failed (13: Permission denied)
</source>
</syntaxhighlight>


= Certbot trouble shooting =
= Let's Encrypt =


<source lang="bash">
<syntaxhighlight lang="bash">
sudo certbot renew -d wiki.tacosync.com --dry-run
sudo snap install --classic certbot
</source>
sudo certbot
</syntaxhighlight>

Latest revision as of 13:41, 7 September 2026

Quick References

TODO Command
List directory
autoindex on;
Catch all server
server_name _;
Prevent 413 Too Large ...
client_max_body_size 128m;

Install nginx on Ubuntu 24

Install

# Install
sudo apt install nginx

# Check service status
sudo systemctl status nginx

# Check tcp port
ss -lnt4 | grep ':80'

# Edit config
cd /etc/nginx/sites-available
sudo vim my-vhost

Setup virtual host proxy

server {
  server_name my-vhost.xxx.com;
  root        /home/myaccount/vhost/my-vhost
  access_log  /var/log/nginx/my-vhost.access.log;
  error_log   /var/log/nginx/my-vhost.error.log;

  location / {
    proxy_pass http://192.168.25.90:12345;
    proxy_read_timeout 60;
    proxy_connect_timeout 10;
  }
}
cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/my-vhost my-vhost
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl status nginx

# check permission of static file
sudo -u www-data ls -l /home/myaccount/vhost/my-vhost

Add self-signed certificate

cd ~
openssl req -new > cert.csr
openssl rsa -in privkey.pem -out key.pem
openssl x509 -in cert.csr -out cert.pem -req -signkey key.pem -days 1001
cat key.pem >> cert.pem
cp cert.pem /etc/nginx/sites-available
server {
  server_name my-vhost.xxx.com;
  root        /home/myaccount/vhost/my-vhost
  access_log  /var/log/nginx/my-vhost.access.log;
  error_log   /var/log/nginx/my-vhost.error.log;
  ssl_certificate     /etc/nginx/sites-available/cert.pem;
  ssl_certificate_key /etc/nginx/sites-available/cert.pem;

  location / {
    proxy_pass http://192.168.25.90:12345;
    proxy_read_timeout 60;
    proxy_connect_timeout 10;
  }
}

Certbot 2.x (Let's Encrypt)

sudo snap install --classic certbot
# sudo ln -s /snap/bin/certbot /usr/bin/certbot

Install nginx on macOS 10.13+

Default nginx package in Homebrew is missing many modules.

Tap 3rd party package is useful for development.

brew tap denji/nginx

brew install nginx-full \
  --with-echo-module \
  --with-autols-module \
  --with-geoip2-module \
  --with-http2 \
  --with-upload-module \
  --with-upload-progress-module

Running nginx as nobody is inconvenient for development. Running as login account is better.

/usr/local/etc/nginx/nginx.conf:2

#     user      group
user  myaccount admin;

When using nobody, response would be 403 forbidden and hard to debug.

Like this: (/usr/local/var/log/nginx/error.log)

2019/02/18 15:42:47 [crit] 25478#0: *78 stat() "/Users/myaccount/Documents/0x01.Source/myvhost/public" failed (13: Permission denied)

Let's Encrypt

sudo snap install --classic certbot
sudo certbot