nginx配置文件

ip设备
192.168.89.130NGINX
192.168.89.129PHP

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.130nginx
192.168.89.129apache

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nginx 配置文件是一个文本文件,通常位于 `/etc/nginx/` 目录下,主要包括以下几个部分: 1. 全局块:配置影响 nginx 全局的指令,一般有运行 nginx用户组、nginx 进程 pid 存放路径、日志存放路径和类型以及配置文件引入等。 2. events 块: 配置影响 nginx 服务器或与用户的网络连接,常用于设置连接超时时间、最大连接数等。 3. http 块:http 块中定义的配置指令用于处理 Web 请求,主要包括了 MIME 类型、字符集、缓存、请求限制等。 4. server 块:配置虚拟主机的相关参数,一个 http 块中可以包含多个 server 块,每个 server 块就相当于一个虚拟主机,用于处理来自客户端的请求。 一个简单的 Nginx 配置文件示例如下: ``` user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html index.htm; } } } ``` 这个配置文件包含全局块和一个 server 块,其中: - `user` 指定 Nginx 运行的用户; - `worker_processes` 指定 Nginx 启动的 worker 进程数; - `error_log` 指定错误日志文件路径和级别; - `pid` 指定 Nginx 进程 ID 存放路径; - `events` 块中 `worker_connections` 指定每个 worker 进程最大连接数; - `http` 块中 `include` 指定 MIME 类型配置文件路径; - `default_type` 指定默认 MIME 类型; - `access_log` 指定访问日志文件路径和格式; - `sendfile` 指定是否使用 sendfile 函数传输文件; - `keepalive_timeout` 指定 keep-alive 连接超时时间; - `server` 块中 `listen` 指定监听端口和协议; - `server_name` 指定虚拟主机域名; - `location` 指定请求 URL 和对应的文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值