nginx安装
[root@localhost nginx-1.24.0]#yum install gcc pcre pcre-devel zlib zlib-devel -y
[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.24.0]#make
[root@localhost nginx-1.24.0]#make install
启动nginx
./nginx 启动
./nginx -s stop 停止
./nginx -s quit 优雅停止,在推出前完成已接受的连接请求
./nginx -s reload 重新加载配置
cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx
[root@localhost sbin]#firewall-cmd --add-port=80/tcp --permanent
[root@localhost sbin]#firewall-cmd --reload
[root@localhost sbin]#ps -ef | grep nginx
完成系统服务
创建服务脚本
vim /usr/lib/systemd/system/nginx.service
服务脚本内容
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
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
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WanteBy=multi-user.target
重新加载系统服务
[root@localhost sbin]#systemctl daemon-reload
nginx配置文件解析
最小配置
#默认为1,表示开启一个业务进程,开启一个work进程;看cpu核心数分配
worker_processes 1;
events {
#单个业务进程可接受连接数(每个work进程可接受连接数)
worker_connections 1024;
}
http {
# 引入http mime类型(mime.types里面存储请求头类型)
include mime.types;
# 如果mime类型没有匹配上,默认使用二进制流的方式传输
default_type application/octet-stream;
#使用linux的sendfile(socket,file,len)搞笑网络传输,也就是0拷贝
sendfile on;
#连接超时
keepalive_timeout 65;
#虚拟主机
server {
#监听端口号
listen 80;
#主机、域名(vhost)
server_name localhost;
#uri
location / {
#目录
root html;
#默认页
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
域名解析
[root@localhost ~]# cd
[root@localhost ~]# mkdir www
[root@localhost www]# cd www
[root@localhost www]# mkdir www
[root@localhost www]# mkdir vod
[root@localhost www]# cd vod
[root@localhost vod]# echo "this is vod web site." >> index.html
[root@localhost vod]# cd ..
[root@localhost www]# cd www
[root@localhost www]# echo "this is www web site." >> index.html
[root@localhost www]# vim /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.mmban.com;
location / {
root /root/www/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name vod.mmban.com;
location / {
root /root/www/vod;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
windows主机hosts配置如下:
虚拟机所装系统IP地址 www.mmban.com
虚拟机所装系统IP地址 vod.mmban.com
ServerName匹配规则
server_name可以配置多个站点,如:
server_name vod.mmban.com vod1.mmban.com;
server_name支持通配符配置,如下:
server_name *.mmban.com;
server_name www.mmban.*;
server_name支持正则匹配,如下:
server_name ~^[0-9]+\.mmban\.com$;
反向代理
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
#填写要代理的网站地址
proxy_pass http://www.gaoqiulin.top;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
负载均衡
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream webs{
server 192.168.6.134:80;
server 192.168.6.135:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://webs;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
权重
upstream webs{
server 192.168.6.134:80 weight=8;
server 192.168.6.135:80 weight=2;
}
upstream webs{
#加上down这台机器不参与负载均衡
server 192.168.6.134:80 weight=8 down;
server 192.168.6.135:80 weight=2;
}
upstream webs{
server 192.168.6.134:80 weight=8;
#加上backup说明该机器是备用机
server 192.168.6.135:80 weight=2 backup;
}
动静分离
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.6.134:8080;
}
location /js{
root html;
}
location /img {
root html;
}
location /css {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
使用正则匹配
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.6.134:8080;
}
location ~*/(js|img|css) {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
URLRewrite
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
proxy_pass http://192.168.6.134:8080;
}
location ~*/(js|img|css) {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
评论区