Nginx安装
- 下载压缩包
wget http://nginx.org/download/nginx-1.18.0.tar.gz
- 解压
tar -zxvf nginx-1.18.0.tar.gz
cd ./nginx-1.18.0
- 安装编译环境
yum -y install gcc automake autoconf libtool make gcc-c++
yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
- 进入解压好的目录执行配置
./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
- 创建文件目录
mkdir /var/temp/nginx/client -p
- 编译安装
make
make install
- 进入到 sbin目录,执行
nginx命令
cd /usr/local/nginx/
./nginx 启动
./nginx -s stop 关闭
ps aux | grep nginx 查看进程
虚拟主机
1.配置文件
- Nginx核心配置文件说明
# work的进程数,默认为1
worker_processes 1;
# 配置影响nginx服务器与用户的网络连接
events {
# 单个work 最大并发连接数
worker_connections 1024;
}
# http块是配置最频繁的部分 可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能
http {
# 引入mime类型定义文件
include mime.types;
default_type application/octet-stream;
sendfile on;
# 超时时间
keepalive_timeout 65;
# server 配置虚拟主机的相关参数 可以有多个,一个server就是一个虚拟主机
server {
# 监听的端口
listen 80;
# 监听地址
server_name localhost;
# 默认请求配置
location / {
# 默认网站根目录
root html;
# 欢迎页
index index.html index.htm;
}
# 错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2.端口指定虚拟主机
- 在nginx.conf中添加一个新的server
http {
server {
listen 80;
server_name localhost;
location / {
root html1;
index index.html index.htm;
}
}
server {
# 修改端口
listen 81;
server_name localhost;
location / {
root html2;
index index.html index.htm;
}
}
}
3.域名指定虚拟主机
- 在nginx.conf中添加一个新的server
http {
server {
listen 80;
server_name a.hzlim.com;
location / {
root html1;
index index.html index.htm;
}
}
server {
listen 80;
# 修改域名
server_name b.hzlim.com;
location / {
root html2;
index index.html index.htm;
}
}
}
4.配置跨域
- 在location中添加请求头
http {
server {
listen 80;
server_name localhost;
location / {
# 添加跨域请求头
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
root html;
index index.html index.htm;
}
}
}
5.配置SSH代理
- 在server中添加配置
http {
server {
# 监听https的端口
listen 443;
server_name localhost;
# 配置证书路径
ssl_certificate cert/server.pem;
ssl_certificate_key cert/server.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
}
反向代理
- 反向代理服务器的配置
http {
# 反向代理配置,upstream中的server是真正处理请求的应用服务器地址
upstream my-server{
# 用server定义HTTP地址
server 192.168.1.101:8080;
}
server {
listen 80;
server_name www.hzlim.cn;
location / {
# 转发到的地址,利用 proxy_ pass可以将请求代理到upstream命名的HTTP服务
proxy_pass http://my-server;
index index.html index.htm;
}
}
}
负载均衡
1.轮询
- 默认策略, 每个请求按照时间顺序逐一分配到不同的服务器
http {
upstream my-server{
# 配置多个服务的地址
server 192.168.1.101:8080;
server 192.168.1.102:8080;
}
server {
listen 80;
server_name www.hzlim.cn;
location / {
proxy_pass http://my-server;
index index.html index.htm;
}
}
}
2.权重
- 根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1
http {
upstream my-server{
# 配置多个服务的地址
server 192.168.1.101:8080 weight=1;
server 192.168.1.102:8080 weight=10;
}
server {
listen 80;
server_name www.hzlim.cn;
location / {
proxy_pass http://my-server;
index index.html index.htm;
}
}
}
更新时间:{docsify-updated}
