博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx服务各功能实现方法
阅读量:2164 次
发布时间:2019-05-01

本文共 4272 字,大约阅读时间需要 14 分钟。

Nginx服务各功能实现方法


文章目录


一、Nginx的状态统计

1.安装nginx时将 --with-http_stub_status_module 模块开启

2.修改nginx配置文件/usr/local/nginx/conf/nginx.conf:(写入要访问的server标签中)

# vim /usr/local/nginx/conf/nginx.conf	location /nginx_status{
#写在某一server标签中,nginx_status为自定义的关键词 stub_status on; #开启状态统计模块 access_log off; #状态统计页面的访问无须记录到日志中 }

3.客户端访问网址:http://IP/nginx_status:(在配置文件中定义什么关键词就访问什么,即可进入统计页面)

在这里插入图片描述

二、Nginx的目录保护(进入网站时先验证身份)

1.在配置文件/usr/local/nginx/conf/nginx.conf某网站页面的location中添加两条配置:

auth_basic "Welcome to nginx_status!";		auth_basic_user_file /usr/local/nginx/html/htpasswd.nginx; 		#验证账号密码的文件所在的位置

例如:

在这里插入图片描述

2.使用Apache的命令htpasswd进行用户密码文件的创建:(生成在上面指定的位置)

①下载RPM包httpd:

# yum -y install httpd

②创建用户密码文件:

# htpasswd -c /usr/local/nginx/html/htpasswd.nginx 用户名 #所在位置与配置文件中要一致

3.重启nginx并再次访问页面即可:

# pkill -HUP nginx

三、Nginx的访问控制

1.在配置文件/usr/local/nginx/conf/nginx.conf某网站页面的location中添加以下设置:

allow 192.168.88.1;       #允许此IP,注意填写客户端IP(Windows)		deny 192.168.88.0/24;     #拒绝88网段		#仅允许192.168.88.1访问服务器(允许个别,拒绝所有)

2.重启nginx并再次访问页面即可:

# pkill -HUP nginx

四、Nginx的虚拟主机

1.准备两个域名并使用本地hosts文件进行解析(www.sohu.com、www.sina.com)

2.在配置文件/usr/local/nginx/conf/nginx.conf中并列编写两个server标签,并分别写好各自信息:

# vim /usr/local/nginx/conf/nginx.confserver {
listen 80; server_name blog.kernel.com; #域名 index index.html index.htm index.php; #默认加载的网页 root html/blog; #网页根目录 access_log logs/blog-access.log main; #访问日志存放位置及格式}server {
listen 80; server_name bbs.kernel.com; index index.html index.htm index.php; root html/bbs; access_log logs/bbs-access.log main;}

3.在/html/目录下分别创建两个目录,并在新建目录内创建index.html文件(分别写不一样的内容)

4.用浏览器分别访问两个不同的域名验证结果即可

五、Nginx的反向代理

在这里插入图片描述

1.在nginx服务器的配置文件/usr/local/nginx/conf/nginx.conf中添加以下location标签:(写在某一个网站的server标签内)

# vim /usr/local/nginx/conf/nginx.conf	location ~ \.php$ {
#删除此标签内原有的内容 proxy_pass http://192.168.88.100:80; #此处填写apache服务器的IP地址 }

2.重启nginx,并使用浏览器访问测试效果

# nginx -t# pkill -HUP nginx

六、Nginx的负载均衡

1.使用默认的rr轮训算法,修改nginx配置文件/usr/local/nginx/conf/nginx.conf:

# vim /usr/local/nginx/conf/nginx.confupstream bbs {
#声明服务器组,此标签在server标签前添加,在sever标签外面 server 192.168.88.100:80 weight=1; #1和2代表给两台服务器分配请求的比例 server 192.168.88.200:80 weight=2;}server {
........; #修改自带的location / 的标签,将原内容删除,添加下列两项 location / {
#任何访问使用反向代理 proxy_pass http://bbs; #轮询,添加反向代理,代理地址填写upstream声明的组名字 proxy_set_header Host $host; #重写URL头部地址,保证网站所有页面都可访问成功(若需要调用数据库一定要开此条)}

2.重启nginx,并使用浏览器访问测试:

# nginx -t# pkill -HUP nginx

七、Nginx实现https(证书+rewrite)

1.安装nginx时,需要将–with-http_ssl_module 模块开启

2.生成证书和秘钥文件:

①在/usr/local/nginx/conf目录下创建ssl目录

# cd /usr/local/nginx/conf# mkdir ssl

②在/usr/local/nginx/conf/ssl下生成私钥:

# cd /usr/local/nginx/conf/ssl/# openssl genrsa -out kernel.key 1024#建立服务器私钥,生成RSA密钥

③在/usr/local/nginx/conf/ssl下生成未签字证书文件:

# openssl req -new -key kernel.key -out kernel.csr#需要依次输入国家,地区,组织,email。最重要的是有一个common name,可以写你的名字或者域名。如果为了https申请,这个必须和域名吻合,否则会引发浏览器警报。生成的csr文件交给CA签名后形成服务端自己的证书

④使用CA服务器签发证书,设置证书的有效期等信息:(自签)

# openssl x509 -req -days 365 -sha256 -in kernel.csr -signkey kernel.key -out kernel.crt#生成签字证书

3.修改配置文件/usr/local/nginx/conf/nginx.conf:

# vim /usr/local/nginx/conf/nginx.confserver {
#在要加密的server标签内添加以下内容 .......; listen 443; #修改端口为443,加密信息必须在443端口下才能生效 ssl on; ssl_certificate /usr/local/nginx/conf/ssl/kernel.crt; ssl_certificate_key /usr/local/nginx/conf/ssl/kernel.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";}

4.重启Nginx服务:

# nginx -t# pkill -HUP nginx

5.强制跳转https:(有些时候为了安全,网站不允许使用http访问,仅允许使用https访问,目的是为了更加安全)

①修改配置文件/usr/local/nginx/conf/nginx.conf中原有的server标签修改监听端口:

# vim /usr/local/nginx/conf/nginx.confserver {
..........; listen 443; #将ssl加密信息所监听的端口改为443}

②在配置文件/usr/local/nginx/conf/nginx.conf新增以下server标签:

# vim /usr/local/nginx/conf/nginx.confserver{
#新建一个server标签 listen 80; #将新建的server标签改为80端口,接收来自80端口的请求 server_name bbs.kernel.com; rewrite ^(.*)$ https://bbs.kernel.com permanent; root html; index index.html index.htm;}

③重启Nginx服务,用浏览器访问网页测试结果:

# nginx -t# pkill -HUP nginx

转载地址:http://nwczb.baihongyu.com/

你可能感兴趣的文章
【LEETCODE】303-Range Sum Query - Immutable
查看>>
【LEETCODE】21-Merge Two Sorted Lists
查看>>
【LEETCODE】231-Power of Two
查看>>
【LEETCODE】172-Factorial Trailing Zeroes
查看>>
【LEETCODE】112-Path Sum
查看>>
【LEETCODE】9-Palindrome Number
查看>>
【极客学院】-python学习笔记-Python快速入门(面向对象-引入外部文件-Web2Py创建网站)
查看>>
【LEETCODE】190-Reverse Bits
查看>>
【LEETCODE】67-Add Binary
查看>>
【LEETCODE】7-Reverse Integer
查看>>
【LEETCODE】165-Compare Version Numbers
查看>>
【LEETCODE】299-Bulls and Cows
查看>>
【LEETCODE】223-Rectangle Area
查看>>
【LEETCODE】12-Integer to Roman
查看>>
【学习方法】如何分析源代码
查看>>
【LEETCODE】61- Rotate List [Python]
查看>>
【LEETCODE】143- Reorder List [Python]
查看>>
【LEETCODE】82- Remove Duplicates from Sorted List II [Python]
查看>>
【LEETCODE】86- Partition List [Python]
查看>>
【LEETCODE】147- Insertion Sort List [Python]
查看>>