0%

安裝Nginx

安裝Nginx

1
$ apt install nginx

config路徑

1
2
3
# 查詢config路徑
$ nginx -t
# ubuntu預設會在/etc/nginx底下

基本指令

1
2
3
4
5
6
# 啟動nginx
$ systemctl start nginx
# 停止ngnix
$ systemctl stop nginx
# 重載ngnix
$ systemctl reload nginx

頁面測試 http://127.0.0.1

以下內容偏向心得筆記

設定proxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
# 監聽 80 port
listen 80;
listen [::]:80;
# 設置進來的domainname或ip
server_name localhost;
# 設置routing
location / {
# 轉發
proxy_pass http://localhost:9001;
# 轉發request的header內容
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
}
}

設定loadbalance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 設置輪巡節點
upstream servicemapping {
server 127.0.0.1:9001 weight=3;
server 127.0.0.1:9002 weight=2;
}

server {
# 監聽 80 port
listen 80;
listen [::]:80;
# 設置進來的domainname或ip
server_name my.app.test;
# 設置routing
location / {
# 轉發
proxy_pass http://servicemapping;
# 轉發request的header內容
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
}
}

兩次請求結果