Nginx 反向代理並作為服務執行
今天被公司一臺自帶nginx Linux伺服器套路了。部署了一個server在伺服器上用的80埠用來測試,沒注意到是否有nginx service。結果公司IT把伺服器重啟了,把80埠給我佔了,導致我server恢復不起來。(⊙o⊙)…,是時候記錄一波部署路線,防止以後翻車了。
測試環境: ubuntu(linux)
文章目錄
1 nginx安裝
1.1 下載原始碼
下載nginx原始碼
http://nginx.org/ 官方網站下載最新版本程式碼
版本:1.13.12
下載openssl 原始碼
https://www.openssl.org/source/old/1.1.0/
版本:1.1.0g
下載pcre
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
版本:8.4.2
1.2 安裝
tar -vxf nginx-1.13.12.tar.gz tar -vxf openssl-1.1.0g.tar.gz tar -vxf pcre-8.42.tar.gz cd nginx-1.13.12 ./configure --with-http_ssl_module --prefix=/home/ubuntu/webvideo/nginx --with-pcre=/home/ubuntu/webvideo/pcre-8.42 --with-openssl=/home/ubuntu/webvideo/openssl-1.1.0g make && make install
安裝完成後在安裝目錄下有conf html logs sbin 這四個檔案
2 配置反向代理及部署
nginx需要做相應配置才能使用,進入nginx 配置檔案vi conf/nginx.conf
2.1 預設nginx config
Nginx.conf中一般情況下內容如下:(略做刪減,加點註釋免得過一陣看又一臉懵逼)
#user nobody; worker_processes 1; #允許生成的程序數,預設為1 #error_log logs/error.log; #全域性log #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { #events塊 worker_connections 1024; #最大連結數 } http { #http全域性塊 include mime.types; #副檔名與檔案型別對映表 default_type application/octet-stream; #預設檔案型別 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #允許sendfile方式傳輸檔案 #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #連線超時時間 #gzip on; server { #server塊 listen 80; #監聽埠 server_name localhost; #監聽地址 #charset koi8-r; #access_log logs/host.access.log main; location / { root 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 html; } } }
2.2 配置反向代理
我們主要在http塊中 新增一個server 作為反向代理。
server {
listen 80; #監聽埠
location / {
root html;
#index index.html index.htm;
index ismarthome.html;
}
location ~ .*\.(htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|woff|ttf|mov|MOV)$ {
#請求的url過濾,正則匹配,任何以上訴字尾的檔案均代理到3017
expires 24h;
proxy_pass http://localhost:3017;
}
location /message { #message 路由代理到3017埠server
proxy_pass http://localhost:3017/oauth2;
}
location /users{ #users路由代理到3017埠server
proxy_pass http://localhost:3017/users
}
location /administration { #administration 路由代理到3017埠server
proxy_pass http://localhost:3017/administration;
}
}
2.3 執行nginx並測試
2.3.1 執行nginx
在安裝目錄sbin下:
執行./nginx 啟動nginx
其他命令:
./nginx -s signal
• stop — fast shutdown
• quit — graceful shutdown
• reload — reloading the configuration file
• reopen — reopening the log files
./nginx -t 檢查配置檔案的語法問題
2.3.2 測試方向代理
http://XXX.XX.XX.XX/users 訪問該路由成功代理到3017埠上的express中
得到測試路由的response:
respond with a resource
反向代理成功。
3 nginx作為linux service執行
nginx作為linux系統服務可以隨系統啟動和關閉實現自動啟動關閉,防止引伺服器重啟帶來的問題。
然後從nginx 官網連結上找到適配debian 環境 init 檔案:
http://kbeezie.com/debian-ubuntu-nginx-init-script/
略做修改,主要是修改nginx安裝字首及nginx.pid檔案位置使之適配nginx confg
- 建立/etc/init.d/nginx
- 複製下列中配置檔案並將中的opt 改為nginx安裝目錄
- sudo service nginx start 啟動nginx
- sudo service nginx stop 停止nginx
配置檔案如下:/etc/init.d/nginx
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
opt=/home/ubuntu/webvideo/nginx
PATH=$opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=$opt/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile $opt/logs/nginx.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile $opt/logs/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
$opt/logs/nginx.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
$opt/logs/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile $opt/logs/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
systemctl status nginx.service 檢視nginx 服務狀態
systemctl daemon-reload 如更改 init.d/nginx檔案, 需要使用此命令重啟