nginx基础知识

一、nginx的安装

linux上的软件我大部分都放在/opt/modules目录下面,这里我把Nginx安装在/usr/local/nginx下面.

1.首先安装c语言环境

yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel

2.安装nginx

首先把nginx安装包上传到/opt/modules下面,通过tar -zxvf 压缩包解压,然后进入解压后的文件,进行编译和安装。对于已经安装过Nginx,然后需要添加其他模块,在configure后面添加就行了,然后执行make就行了,把objs下的nginx文件替换下就能添加新模块了.

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

3.把nginx安装成系统服务

vi /usr/lib/systemd/system/nginx.service
服务脚本内容
[Unit]
Description=nginx - web server
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]
WantedBy=multi-user.target

重新加载系统服务

systemctl daemon-reload

启动服务

systemctl start nginx.service

开机自启

systemctl enable nginx.service

二、nginx基础知识

1.servername匹配规则

完整匹配
我们可以在同一servername种匹配多个域名

server_name  hsh1.com hsh2.com

通配符匹配

sever_name  *.hsh.com

通配符结束匹配

server_name hsh.*

正则匹配

server_name ~^[0-9]+\.hsh\.com$;

2.反向代理配置

在location中添加proxy_pass

location / {
proxy_pass http://hshd/;
}

在server上面添加upstream

upstream hshd {
server 192.168.44.102:80;
server 192.168.43.103:80;
}

3.负载均衡策略

轮询
默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求.
weight(权重)
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况

upstream httpd {
server 127.0.0.1:8050 weight=10;
server 127.0.0.1:8060 weight=1 backup;
}
  • down:表示当前的server暂时不参与负载
  • weight:默认为1.weight越大,负载的权重就越大。
  • backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。

ip_hash
根据客户端的ip地址转发同一台服务器,可以保持回话。
url_hash
根据用户访问的url定向转发请求

4.动静分离

1.nginx上配置静态资源
直接把静态资源放到nginx上,然后添加一个location来配置静态资源的请求

location ~*/(css|img|js) {
        root /usr/local/nginx/static;
        index index.html index.htm;
}

5.alias与root

root的话会加上location后面的路径,而alias不会

location /css {
           alias /usr/local/nginx/static/css;
          index index.html index.htm;
}

6.centos7防火墙开启与关闭

应用服务器防火墙配置

开启防火墙    systemctl start firewalld
重载规则    firewall-cmd --reload
查看已配置规则   firewall-cmd --list-all
指定端口和ip访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.44.101"
port protocol="tcp" port="8080" accept"
移除规则
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source
address="192.168.44.101" port port="8080" protocol="tcp" accept"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzahxa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值