ip | 设备 |
---|---|
192.168.89.130 | NGINX |
192.168.89.129 | PHP |
1.配置ip访问控制
allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny:设定不允许哪台或哪些主机访问,多个参数间用空格隔开
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 192.168.89.129:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
allow 192.168.89.129/32; #允许192.168.89.129的设备访问
deny all; 不允许除了上面设置的ip的机器ip以外的任何ip访问
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
效果
修改配置文件,允许89的网段访问
[root@localhost conf]# vim nginx.conf
allow 192.168.89.0/24;
deny all;
效果
2.nginx基于用户认证
location ~ \.php$ {
root html;
fastcgi_pass 192.168.89.129:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
auth_basic "用户提示信息";
auth_basic_user_file "/path/to/user_auth_file" ; 用户密码加密文件路径
}
我们使用htpasswd创建加密文件,需要先安装Apache的发行包
sudo yum install -y http 安装Apache的发行包
[root@localhost nginx]# htpasswd -c -m /path/to/.user_auth_file USERNAME
New password: 设定密码
Re-type new password:
Adding password for user kongbai
-c:创建一个新的文件
-m:使用md5算法进行加密
USERNAME:用户名
/path/to/user_auth_file:用户密码加密文件路径
效果
使用密码登录
3.nginx证书
3.1新建证书存放目录
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# chown -R nginx.nginx ssl/
3.2在目录下生成RSA私钥
[root@localhost ssl]# openssl genrsa -des3 -out cert.key 1024 CERT可为自定义名称
Generating RSA private key, 1024 bit long modulus
....++++++
...........................++++++
e is 65537 (0x10001)
Enter pass phrase for cert.key:
Verifying - Enter pass phrase for cert.key:
3.3创建csr证书
[root@localhost ssl]# openssl req -new -key cert.key -out cert.csr
按照命令依次输入内容
输入私钥密码------->国家(CN)------>省份(XB)----->城市(WH)------>公司名称------->组织-------->公司域名-------->邮箱-------->私钥密码
[root@localhost ssl]# ls
cert.csr cert.key
3.4生成crt证书
[root@localhost ssl]# openssl x509 -req -days 7 -in cert.csr -signkey cert.key -out cert.crt
Signature ok
subject=/C=CN/ST=HB/L=WH/O=kongbai.com/OU=kongbai.com/CN=www.kongbai.com/emailAddress=664506081@qq.com
Getting Private key
3.5配置nginx证书
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/local/nginx/ssl/cert.pem; 这里生成的证书路径和名称必须一致
ssl_certificate_key /usr/local/nginx/ssl/cert.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
网站输入https://www.kongbai.com/index.html 必须在前面加上httpd://
4.开启status
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
location /status {
stub_status on;
allow 192.168.89.0/24;
deny all;
}
[root@localhost conf]# nginx -s reload
页面访问方式:ip地址+/status
** 第1列:**
当前与http建立的连接数,包括等待的客户端连接:2
第2列:
接受的客户端连接总数目:44
处理的客户端连接总数目:44
客户端总的请求数目:83
第3列:
当前,nginx读请求连接
当前,nginx写响应返回给客户端
目前有多少空闲客户端请求连接
5.rewrite
rewrite <regex> <replacement> [flag];
关键字 正则 替代内容 flag标记
常见的flag
flag | 作用 |
---|---|
last | 本条规则匹配完成后继续向下匹配新的location URI规则 |
break | 本条规则匹配完成后终止,不在匹配任何规则 |
redirect | 以临时重定向的HTTP状态302返回新的URL |
permanent | 以永久重定向的HTTP状态302返回新的URL |
nginx使用的语法源于Perl兼容正则表达式库,基本语法如下
标识符 | 意义 |
---|---|
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
. | 匹配任意字符 |
* | 匹配前面的字符零次或者多次 |
+ | 匹配前面字符串一次或者多次 |
? | 匹配前面的字符零次或者一次 |
[] | 匹配指定字符集内的任意字符 |
[^] | 匹配任何不包括在指定字符集内的任意字符 |
() | 分组,组成一组用于匹配的实体 |
^(hello|sir)$ 字符串为“hello sir”捕获的结果$1=hello;$2=sir
server {
listen 80;
server_name www.kongbai.com; 这是本地ip转换的域名,所以在网页输入ip地址就可以冲定向
rewrite ^i(.*) https://www.kongbai.com$1 redirect; 把http协议重定向到https上面
6.负载均衡,反向代理
ip | 设备 |
---|---|
192.168.89.133 | 负载均衡服务器 |
192.168.89.130 | nginx |
192.168.89.129 | apache |
6.1 192.168.89.129安装apache
[root@localhost ~]# yum install -y httpd
[root@localhost html]# cd /etc/httpd/conf
[root@localhost conf]# vim httpd.conf
ServerName www.example.com:80 将这一行前面的#好去掉
[root@localhost ~]# cd /var/www/html/ httpd默认默认主页存放目录
[root@localhost html]# vim index.html
hello world
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost html]# systemctl start httpd
[root@localhost html]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
6.2 192.168.89.130安装nginx
[root@localhost ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 添加nginx的源
[root@localhost ~]# yum install -y nginx
[root@localhost html]# pwd
/usr/share/nginx/html
[root@localhost html]# mv index.html index.html.1
[root@localhost html]# vim index.html
world hello
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
6.3 192.168.89.133作为负载均衡的服务器,应该安装nginx,我这里是编码安装的
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
···
upstream kongbai { 在server前面加这个,ip为设置nginx和apache的服务器ip
server 192.168.89.130;
server 192.168.89.129;
}
server {
设置upstream完成后再server添加
server {
location / { 把原有location内容删除
proxy_pass http://kongbai; 这里的kongbai是前面upstream设置的名称
}
}
[root@localhost conf]# nginx -t 检查配置文件是否出错
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# nginx -s reload
7.测试
刷新下
8.负载均衡其他配置
upstream kongbai {
ip_hash; 根据客户端ip,计算hash值并映射到相应服务器。那么同一个用户就始终申请同一个服务器上的服务,这样的话就保证了用户信息始终一致。
server 192.168.89.130 weight=5; weight加载后面,刷新网页就是刷新5次,显示5次后变成129显示的网页,而129刷新两次就跳回到130
server 192.168.89.129 weight=2;
}
9.if
语法:if (cindition) {...}
应用场景:server段和location段
9.1常见的condition
•变量名(变量值为空串,或者以“0”开始,则为false,其它的均为true)
•以变量为操作数构成的比较表达式(可使用=,!=类似的比较操作符进行测试)
•正则表达式的模式匹配操作
• 〜:区分大小写的模式匹配检查
• 〜:不区分大小写的模式匹配检查
• !〜和!〜*:对上面两种测试取反
•测试指定路径为文件的可能性(-f,!-f>
•测试指定路径为目录的可能性(-d,!-d)
•测试文件的存在性(-e,!-e)
•检查文件是否有执行权限(-x,!-x)