Nginx 入门 - 安装与基本配置
Nginx 入门 - 安装与基本配置
目录
1 安装
1.1 Linux 下安装环境配置
// 1 c语言编译环境
yum install gcc-c++
// 2 PCRE 解析http模块正则表达式
yum install -y pcre pcre-devel
// 3 zlib 对http包内容进行gzip
yum install -y zlib zlib-devel
// 4 openssl 安全套接字密码库
yum install -y openssl openssl-devel
1.2 Nginx 快速安装
// 1 xftp 上传 nginx.tar 包到Linux
// 2 解压tar 包
tar -xvf nginx-1.17.8.tar
// 3 进入 nginx 创建临时目录
mkdir /var/temp/nginx/client -p
// 4 执行config 命令生成 Mikefile \ 代表拼接,以下命令是一行命令,执行完毕后生成MakeFile 文件
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
// 5 编译并安装
make
make install //Nginx 安装目录如下
// make[1]: 进入目录“/root/nginx-1.17.8”
// test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
// 6 进入nginx 目录的sbin 目录中启动
./nginx 启动
./nginx -s stop 关闭
./nginx -s reload 重启
ps aux | grep nginx 查看进程
1.3 配置虚拟主机
1.3.1 Notepad ++ 连接 Linux
// 1 插件 插件管理 搜索NppFTP 安装
// 2 插件 - NppFTP - show - setting - profile - sftp
1.3.2 通过端口区分虚拟主机
// 1 修改 nginx.conf
// 添加新 server 配置
server {
listen 81; # 修改端口
server_name localhost;
location / {
root html81; # 重新制定一个目录
index index.html index.htm;
}
}
// usr/local/nginx 目录下创建对应html81 目录
1.3.3 通过域名区分虚拟主机
// 1 修改windows hosts文件 C:\Windows\System32\drivers\etc
// 配置域名和ip的映射关系
// 2 配置nginx.conf
server {
listen 80;
server_name www.jd.com;
location / {
root jd;
index jd.html;
}
}
// 一台服务器可通过访问不同域名访问不同内容
1.4 反向代理
// 1 安装2个tomcat模拟两台http服务器,并修改端口和启动tomcat
// 2 反向代理服务器配置 nginx.conf
upstream lagou1{
#用server定义HTTP地址
server 192.168.80.100:8080;
}
server {
listen 80;
server_name www.lagou2.com;
location / {
proxy_pass http://lagou2;
index index.html index.htm;
}
}
// 3 hosts文件添加域名和ip映射
// 4 nginx 重启加载配置文件
nginx -s reload
1.5 负载均衡
// 轮询模式
// 1 配置 nginx.conf
upstream lagouedu{
#用server定义HTTP地址 配置两个
server 192.168.80.100:8080;
server 192.168.80.100:8081;
}
server {
listen 80;
server_name www.lagouedu.com;
location / {
proxy_pass http://lagouedu;
index index.html index.htm;
}
}
// 2 修改hosts文件
192.168.80.100 www.lagouedu.com
// 3 重启
nginx -s reload
// 权重模式
// 1 配置 nginx.comf
upstream lagouedu{
#用server定义HTTP地址 配置两个
server 192.168.80.100:8080 weight=1;
server 192.168.80.100:8081 weight=10;
}
server {
listen 80;
server_name www.lagouedu.com;
location / {
proxy_pass http://lagouedu;
index index.html index.htm;
}
}
// 2 修改hosts文件
192.168.80.100 www.lagouedu.com
// 3 重启
nginx -s reload
1.6 发布项目并配置
// 项目打war包后改名上传到Linux下tomcat webapps 目录下
// 配置nginx 反向代理和负载均衡
// 项目上传成功无法访问报错
problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionErrorUnsupported major.minor version 51.0
// Linux jdk 版本和 项目 jdk环境不同

浙公网安备 33010602011771号