1、下载解压nginx
cd /usr/local/nginx
// wget https://nginx.org/download/nginx-1.9.9.tar.gz #下载(有了就不用下载)
tar -zxvf nginx-1.9.9.tar.gz #解压
或者直接从官网(Index of /download/)下载放入宿主机目录/usr/local/nginx
2、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel
make # 遍历
zlib、zlib-devel # 主要用于文件的解压缩
gcc、gcc-c++ # 主要用来进行编译相关使用
openssl、openssl-devel # 一般当配置https服务的时候就需要这个了
pcre、pcre-devel # Nginx的rewrite模块和HTTP核心模块会用到PCRE正则表达式语法
3、编译并安装nginx
cd nginx-1.9.9
//编译
./configure --prefix=/usr/local/nginx
//安装
make && make install
4、简单配置
server {
listen 80;
server_name localhost;
location / {
root /usr/local/web/dist;
index index.html index.htm;
}
# 静态资源目录,在对应目录先建好文件夹
location /file{
alias /usr/local/web/admin/dist;
index index.html index.htm;
}
#代理node服务
location /api {
proxy_pass http://127.0.0.1:8080;
}
}
5、同一个ip配置多个域名
server{
listen 80;
server_name www.abc.com; #绑定域名
index index.htm index.html index.php; #默认文件
root /home/web/dist; #网站根目录
error_page 404 /404.html;#添加404网页
}
server{
listen 80;
server_name www.bbb.com; #绑定域名
index index.htm index.html index.php; #默认文件
root /home/web/dist1; #网站根目录
}
#不带www的域名加301跳转
server{
listen 80;
server_name bbb.com;
rewrite ^/(.*) http://www.bbb.com/$1 permanent;
}
6、校验nginx.conf配置是否正确
/usr/local/nginx/sbin/nginx -t
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
//代表成功
7、启动、停止、退出、重启
cd /usr/local/nginx/sbin/
./nginx #启动
./nginx -s stop #停止
./nginx -s quit #退出
./nginx -s reload #重启
8、启动后查看nginx进程
ps -ef|grep nginx #查询进程号
kill -QUIT 主进程号 #从容停止
kill -TERM 主进程号 #快速停止
kill -9 主进程号 #强制停止