筆記

Install Nginx

Add nginx ppa source

1
2
3
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx

建立設定檔

Add site config

1
sudo vim /etc/nginx/sites-available/example.com

Edit file /etc/nginx/sites-available/example.com

靜態網站範例

1
2
3
4
5
6
7
8
9
server {
listen 80; ## listen for ipv4; this line is default and implied
# listen [::]:80 default ipv6only=on; ## listen for ipv6

root /var/www/example.com/public_html;
index index.html index.htm;

server_name example.com;
}

搭配 SPA 範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80;
server_name example.com;

location / {
root /home/ubuntu/code/example.com/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

搭配 node.js 範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;

server_name example.com;

location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

Create a link to sites-enable and remove default config

1
2
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
sudo rm /etc/nginx/sites-enabled/default

Restart service and show status

1
2
sudo service nginx restart
sudo service nginx status

防火牆設定

// TODO

參考連結

refs.