logoOutUI

⌘ K
  • 指南
  • 组件
  • 工具类
  • 其他
v5.3.x
  • 其他
    • 安装Nginx
    • 安装Nodejs
    • 安装PM2
最后更新时间:
安装Nodejs
Copyright © 2021-present 粤ICP备2021136339号
‌
‌
‌
‌

Nginx

安装依赖

安装 gcc,编译nginx源码

yum install -y gcc-c++

安装 PCRE pcre-devel,nginx 的 http 模块使用 pcre 来解析正则表达式

yum install -y pcre pcre-devel

安装 zlib,nginx 使用zlib对http包的内容进行gzip

yum install -y zlib zlib-devel

安装 OpenSSL,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)

yum install -y openssl openssl-devel

下载nginx

使用 wget 命令下载

wget -c https://nginx.org/download/nginx-1.22.0.tar.gz

安装

根目录使用ls命令可以看到下载的nginx压缩包,然后解压

tar -zxvf nginx-1.22.0.tar.gz

解压后进入目录

cd nginx-1.22.0

使用默认配置

./configure

如果要指定路径

./configure --prefix=/usr/local/nginx

如果还需要配置http_ssl_module和with-http_stub_status_module

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

编译安装

make
make install

查找安装路径,默认都是这个路径

[root@centos ~]# whereis nginx
nginx: /usr/local/nginx

启动、停止nginx

cd /usr/local/nginx/sbin/

启动

./nginx

停止,直接查找nginx进程id再使用kill命令强制杀掉进程

./nginx -s stop

退出停止,等待nginx进程处理完任务再进行停止

./nginx -s quit

重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效

./nginx -s reload

重启nginx,建议先停止,再启动

./nginx -s stop
./nginx

查看nginx版本

./nginx -v
# nginx version: nginx/1.22.0

查看nginx进程,如下返回,即为成功

[root@centos ~]# ps aux|grep nginx
root 5984 0.0 0.0 112708 976 pts/1 R+ 14:41 0:00 grep --color=auto nginx
root 18198 0.0 0.0 20552 612 ? Ss 11:28 0:00 nginx: master process ./nginx
nobody 18199 0.0 0.0 23088 1632 ? S 11:28 0:00 nginx: worker process

关闭防火墙,登录nginx页面

systemctl stop firewalld
# 在浏览器上输入虚拟机IP即可查看nginx登录页面,默认展示index.html

配置系统环境变量,使得nginx命令能够在其它目录下执行

vim /etc/profile
# 在Path变量上追加sbin目录路径,用:分隔其它变量
PATH=/usr/local/nginx/sbin:$JAVA_HOME/bin:$PATH
# 保存退出后执行,使得修改生效
source /etc/profile

开机自启动

在/usr/lib/systemd/system目录下新建nginx.service文件,内容如下

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

设置开机自启动

设置开机自启动:

systemctl enable nginx.service

查看是否正确启动:

systemctl list-unit-files |grep nginx

看下如下就成功启动了:

[root@centor ~]systemctl list-unit-files | grep nginx
nginx.service

启动nginx

systemctl start nginx.service

脚本解释

Unit:服务的启动顺序和依赖关系
Description:对该服务的描述;
After:在b.target服务组启动后,再启动本服务;
Service:服务具体执行的方式
ExecStart,ExecStop,ExecReload等:启动命令组,分别是服务启动时,停止时,重启时,启动前,启动后,停止后执行的命令;
Type:服务启动类型。默认simple表示ExecStart为主进程,notify类似于simple,启动结束后会发出通知信号。另外还有forking,oneshot,dbus,idle等类型;
Install:把服务放在哪个服务组
WantedBy:服务所在的服务组。

其他命令

开启开机自启动

systemctl enable nginx.service

停止开机自启动

systemctl disable nginx.service

启动 nginx 服务

systemctl start nginx.service

停止 nginx 服务

systemctl stop nginx.service

重启 nginx 服务

systemctl restart nginx.service

查看服务当前状态

systemctl status nginx.service

查看所有已启动的服务

systemctl list-units --type=service

nginx配置

进入nginx配置文件目录,找到nginx的配置文件nginx.conf

cd /usr/local/nginx/conf/

nginx.conf详细配置

#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
server_names_hash_bucket_size 64;#这个需要配置,否则启动不起来
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
limit_conn_log_level error;
limit_conn_status 503;
limit_conn_zone $binary_remote_addr zone=one:10m;#one=one或allips 表示设置了名为“one”或“allips”的存储区,大小为10兆字节
limit_conn_zone $server_name zone=perserver:10m;
limit_req_zone $binary_remote_addr zone=allips:100m rate=10r/s;#rate=10r/s 的意思是允许1秒钟不超过10个请求。其中$binary_remote_addr有时需要根据自己已有的log_format变量配置进行替换
server {
listen 80;
server_name localhost; #将localhost修改为您证书绑定的域名,例如:www.example.com。
rewrite ^(.*)$ https://$host$1 permanent; #将所有http请求通过rewrite重定向到https。
# location / {
# root html;
# index index.html index.htm;
# }
}
server {
listen 443 ssl; #配置HTTPS的默认访问端口为443。如果在此处未配置HTTPS的默认访问端口,可能会导致Nginx无法启动。
server_name localhost; #localhost修改为您证书绑定的域名。
ssl_certificate cert/server.crt; #替换成您的证书文件的路径。
ssl_certificate_key cert/server.key; #替换成您的私钥文件的路径。
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5; #加密套件。
ssl_prefer_server_ciphers on;
charset utf8;
#access_log logs/host.access.log main;
location / {
root html/coolyiman-fe/dist;
index index.html index.htm;
}
#后端服务接口代理
location ^~/apis/ {
proxy_pass http://127.0.0.1:8099/;
limit_conn one 100; #limit_conn one 100表示最大并发连接数100
limit_conn perserver 1000;#limit_conn perserver 1000表示该服务提供的总连接数不得超过1000,超过请求的会被拒绝
limit_req zone=allips burst=50 nodelay; #burst=50 表示最大延迟请求数量不大于50。 如果太过多的请求被限制延迟是不需要的 ,这时需要使用nodelay参数,服务器会立刻返回503状态码。
# limit_conn one 40; #连接数限制
#带宽限制,对单个连接限数,如果一个ip两个连接,就是500x2k
#limit_rate 500k;
}
#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;
}
}
}