今天被公司一台自带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; #监听端口
server_name 172.28.28.4;
location /{
proxy_pass http://172.28.28.17:3017;
proxy_redirect off;
}
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文件, 需要使用此命令重启