IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 【Nginx学习笔记】Nginx搭建HTTPS服务、作反向代理服务和静态web服务 -> 正文阅读

[网络协议]【Nginx学习笔记】Nginx搭建HTTPS服务、作反向代理服务和静态web服务

1、Nginx须知

1.1、以下仅仅针对nginx.conf.配置文件的使用

如果需要解决问题,为了节约您的时间,请直接看案例。

1.2、解决的问题

  • 如何使用nginx仅成为代理服务器(只做请求转发)
  • 如何使用nginx搭建HTTPS协议
  • 如何使用nginx成为静态Web服务器

2、Nginx的配置(nginx.conf)

大致的配置如下(根据自己的需求而定):

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           # root   html;
           #index  index.html index.htm;x
           proxy_pass   http://127.0.0.1:9090;	#代理地址
           proxy_set_header  Host $host;
           proxy_set_header   X-real-ip $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


 #  HTTPS server配置
	server {
        listen       443 ssl;
        server_name  localhost;
        ssl			 on;	#开启SSL
        client_max_body_size   0; #设置 不限制 文件上传大小,默认为1M 
        charset utf-8;

        ssl_certificate      /usr/local/ca/local.crt;	#证书绝对路径
        ssl_certificate_key  /usr/local/ca/local.key;	#密钥绝对路径

        ssl_session_cache    shared:SSL:10m;	#设置会话缓存大小
        ssl_session_timeout  10m;	# 连接超时时间

        # ssl_ciphers  HIGH:!aNULL:!MD5;	#支持的算法,默认为OpenSSL的全部算法
        ssl_prefer_server_ciphers  on; #优先使用服务端的SSL密钥证书,默认为off 

	location / {
           proxy_pass   http://127.0.0.1:8080;
           proxy_set_header  Host $host;
           proxy_set_header   X-real-ip $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

}    

}

3、Nginx的结构(配置块)

http {

    server {
        listen       80;
        server_name  localhost;
        
        location / {
        # ····省略
        }
    }
    
    upstream {
        # ····省略
    }
 }

4、Nginx配置静态Web服务器

说明:本机IP:192.168.1.1

案例中的基础配置如下:

    server {
        listen       80;	# 监听的端口号 
        server_name  localhost;	# 主机名(可随便起,一般起具有代表性的词语)

        location / {
			# 以下案例中进行代替
        }
	}

4.1、静态资源请求访问案例1:

**需求:**有一个客户端请求为 /download/index/test.html的URL,实际要访问到本机上 /opt/web/html/download/index/test.html文件的内容。

以下提供两种方法参考:

方法1:

location /download {
	root /opt/web/html;
}

方法2:

location /download/ {
	alias /opt/web/html/download/;
}

解释:请求/download/* 的URL都会转发到本机服务器的 /opt/web/html/download/*

例如:请求 192.168.1.1:80/download/index/test.html 会访问到本机/opt/web/html/download/index/test.html文件的资源。选择使用的方式可根据自己的喜好而定,本人觉得方式1类似于字符地址的拼接,更易于理解,推荐使用。

**温馨提示: **location、root、alias 是关键字不需要改

4.2、静态资源请求访问案例2:

**需求:**访问站点时的URL是/,这时需要返回网站的首页/var/www/path/index.html。

方法1:

location / {
	root /var/www/path;
	index /index.html /index.php /html/index.php
}

解释:访问192.168.1.1:80/的URL,Nginx首先会尝试访问/var/www/path/index.html文件,如果可以访问就直接返回文件内容结束请求,否则再试图返回/var/www/path/index.php文件,以此类推。

4.3、静态资源请求访问案例3:

**需求:**返回错误页面

方法1:

location / {
	root /var/www/path;
	index /index.html /index.php /html/index.php
}

解释:访问192.168.1.1:80/的URL,Nginx首先会尝试访问/var/www/path/index.html文件,如果可以访问就直接返回文件内容结束请求,否则再试图返回/var/www/path/index.php文件,以此类推。

5、Nginx配置代理服务器

说明:本机IP:192.168.1.1

5.1、代理服务器一般案例

请看如下案例1:

 server {
        listen       80;
        server_name  localhost; # 主机名(会与http请求头部的host比较(请求的域名),如果host不为空的话)

        location / {
           proxy_pass   http://127.0.0.1:8080;
           proxy_set_header  Host $host;	# host显示本机的公网地址(192.168.1.1),而不是proxy_pass(127.0.0.1)
           proxy_set_header   X-real-ip $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; #相当于客户端请求头“x-forwarded-for”,并通过逗号添加到变量中
        }
      }

**解释:**来自客户端的请求 http://192.168.1.1:80 会转发到 http://127.0.0.1:8080(也就是Linux本机8080端口),至此完成了代理。

案例1图解如下:
(img-G7Q4swL9-1652660982566)(C:\Users\叔公华\AppData\Roaming\Typora\typora-user-images\1649910763031.png)]


为了进一步了解Nginx代理的关系,请看

案例2:

 server {
        listen       9090;
        server_name  localhost; # 主机名

        location /test {
           proxy_pass   http://192.168.130.133:9999;
           proxy_set_header  Host $host;	
        }
     }

**解释:**监听端口为9090,把路径为/test的请求转发给proxy_pass这个地址;

**完整解释:**客户端请求 http://192.168.1.1/test:9090 转发到 http://192.168.130.133:9999 ,也就是说在浏览器访问 http://192.168.1.1/test:9090 实际上是访问到了 http://192.168.130.133:9999

图解如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7LTGuiN8-1652660982569)(C:\Users\叔公华\AppData\Roaming\Typora\typora-user-images\1649910727534.png)]

5.2、代理服务器SSL案例

先上图解:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zm5YIMST-1652660982570)(C:\Users\叔公华\AppData\Roaming\Typora\typora-user-images\1650330977673.png)]

在代理服务的基础上添加SSL协议(使用HTTPS),本机Linux系统中,有两个服务:1、Nginx服务,2、Tomcat服务。为了以后可以自由拆分或者扩展,我使用了Nginx作为代理服务器,也是本次学习的主要内容。

在config添加以下配置项,

 #  HTTPS server配置
	server {
        listen       443 ssl;
        server_name  localhost;
        ssl			 on;	#开启SSL
        client_max_body_size   0; #设置 不限制 文件上传大小,默认为1M 
        charset utf-8;

        ssl_certificate      /usr/local/ca/local.crt;	#证书绝对路径
        ssl_certificate_key  /usr/local/ca/local.key;	#密钥绝对路径

        ssl_session_cache    shared:SSL:10m;	#设置会话缓存大小
        ssl_session_timeout  10m;	# 连接超时时间

        # ssl_ciphers  HIGH:!aNULL:!MD5;	#支持的算法,默认为OpenSSL的全部算法
        ssl_prefer_server_ciphers  on; #优先使用服务端的SSL密钥证书,默认为off 

	location / {
           proxy_pass   http://127.0.0.1:8080;
           proxy_set_header  Host $host;
           proxy_set_header   X-real-ip $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

}    

**解释:**访问https://192.168.1.1的时候,nginx在收到请求后,做反向代理映射到http://127.0.0.1:8080(Linux本机)上,并且在这过程中会做加密解密的事情。

5.3、根据域名访问Nginx案例

**要求:**已经购买好域名www.zhangsang.com(备案全部弄好之后),并且设置好云解析域名对应IP地址(www.zhangsang.com解析为192.168.1.1)

现有配置如下:

tomcat程序IP地址:127.0.0.1:8080

Linux本机地址:192.168.1.1

在浏览器输入https://www.zhangsang.com可以实际访问到tomcat程序IP地址127.0.0.1:8080

**注意:**www.zhangsang.com和https://www.zhangsang.com是两个不一样的端口,第一个是80端口,第二个是443端口。

主要是配置nginx.config文件如下:

 #  HTTPS server配置
	server {
        listen       443 ssl;
        server_name  www.zhangsang.com;
        ssl			 on;	#开启SSL
        client_max_body_size   0; #设置 不限制 文件上传大小,默认为1M 
        charset utf-8;

        ssl_certificate      /usr/local/ca/local.crt;	#证书绝对路径
        ssl_certificate_key  /usr/local/ca/local.key;	#密钥绝对路径

        ssl_session_cache    shared:SSL:10m;	#设置会话缓存大小
        ssl_session_timeout  10m;	# 连接超时时间

        # ssl_ciphers  HIGH:!aNULL:!MD5;	#支持的算法,默认为OpenSSL的全部算法
        ssl_prefer_server_ciphers  on; #优先使用服务端的SSL密钥证书,默认为off 

	location / {
           proxy_pass   http://127.0.0.1:8080;
           proxy_set_header  Host $host;
           proxy_set_header   X-real-ip $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

}   

5.4、监听同一个端口分别访问到不同的网站案例

**要求:**假设本人购买了两个网站的域名,分别是www.test1.com和www.test2.com,想让两个网站都使用HTTPS协议,但是默认端口(443)只有一个,于是使用nginx可以分别处理映射到不同的端口上去;让www.test1.com的请求转发到127.0.0.1:8080上,而让www.test2.com的请求转发到127.0.0.1:9090上;

**实现思路:**请求访问https://www.test1.com的时候,默认是443端口,也可以把HTTPS指定到其他端口,可是那样做需要把端口号带上例如https://www.test1.com:8080,这样是不是很不方便,所以需要用nginx分发端口号。nginx收到请求的时候首先会找请求头(Response Headers)里面有一个host字段,里面放着请求时的域名或者是ip地址(允许带端口号) ,如果是通过IP访问,那host存放的就是IP,如果是通过域名访问,那存放的就是域名。我们来讨论一下当host存放域名的时候的情况,nginx会根据host的域名(假设是https://www.test1.com)查找监听端口为443,server_name是https://www.test1.com的server配置块,然后代理带对应的proxy_pass

图解如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Esfd5Y9N-1652660982571)(C:\Users\叔公华\AppData\Roaming\Typora\typora-user-images\1650417291974.png)]

**上面图解说明:**对于没有标明域名解析部分,在此特地说明一下,本人已经配置了云解析DNS,记录类型:A,www.test1.com和www.test2.com地址 都是解析到IP 192.168.1.1

nginx.config配置文件如下:

 #  HTTPS server配置
	server {
        listen       443 ssl;
        server_name  www.test1.com;
        ssl			 on;	#开启SSL
        client_max_body_size   0; #设置 不限制 文件上传大小,默认为1M 
        charset utf-8;

        ssl_certificate      /usr/local/ca/local.crt;	#证书绝对路径
        ssl_certificate_key  /usr/local/ca/local.key;	#密钥绝对路径

        ssl_session_cache    shared:SSL:10m;	#设置会话缓存大小
        ssl_session_timeout  10m;	# 连接超时时间

        # ssl_ciphers  HIGH:!aNULL:!MD5;	#支持的算法,默认为OpenSSL的全部算法
        ssl_prefer_server_ciphers  on; #优先使用服务端的SSL密钥证书,默认为off 

	location / {
           proxy_pass   http://127.0.0.1:8080;
           proxy_set_header  Host $host;
           proxy_set_header   X-real-ip $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

}  
	server {
        listen       443 ssl;
        server_name  www.test2.com;
        ssl			 on;	#开启SSL
        client_max_body_size   0; #设置 不限制 文件上传大小,默认为1M 
        charset utf-8;

        ssl_certificate      /usr/local/ca/local.crt;	#证书绝对路径
        ssl_certificate_key  /usr/local/ca/local.key;	#密钥绝对路径

        ssl_session_cache    shared:SSL:10m;	#设置会话缓存大小
        ssl_session_timeout  10m;	# 连接超时时间

        # ssl_ciphers  HIGH:!aNULL:!MD5;	#支持的算法,默认为OpenSSL的全部算法
        ssl_prefer_server_ciphers  on; #优先使用服务端的SSL密钥证书,默认为off 

	location / {
           proxy_pass   http://127.0.0.1:9090;
           proxy_set_header  Host $host;
           proxy_set_header   X-real-ip $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

} 

**以上nginx.config配置文件说明:**证书按理来说要分别使用不同的证书,由于本文只是测试使用,所以就不生成两个证书了,都使用同一个证书。

5.5、限制同一个IP地址的请求数量

http
{
  limit_req_zone $binary_remote_addr zone=mylimit:10m rate=20r/s;//同一个IP每秒20次请求
}

nginx配置只能由域名访问:

server {
    listen 80 default_server;	
    server_name _;	# _ 表示无效域名

    location / {
        return 403;
    }
}

5.6Nginx并发数量

Nginx最大并发数:

max_clients = worker_processes * worker_connections/2

worker_connections解析
1.connections不是随便设置的,而是与两个指标有重要关联,一是内存,二是操作系统级别的“进程最大可打开文件数”。

2.内存:每个连接数分别对应一个read_event、一个write_event事件,一个连接数大概占用232字节,2个事件总占用96字节,那么一个连接总共占用328字节,通过数学公式可以算出100000个连接数大概会占用 31M = 100000 * 328 / 1024 / 1024,当然这只是nginx启动时,connections连接数所占用的nginx。

3.进程最大可打开文件数:进程最大可打开文件数受限于操作系统,可通过 ulimit -n 命令查询,以前是1024,现在是65535,

6、启动Nginx

cd /usr/local/nginx/sbin/	#切换到到nginx的安装目录下
./nginx -t	#检查nginx文件是否正确
./nginx -s reload #优雅地重启nginx
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-05-18 17:59:21  更:2022-05-18 18:00:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 10:37:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码