HTTP协议

概述

  1. 超文本:超链接和各种多媒体元素标记的文本,彼此链接形成web
  2. 超文本标记语言:HTML
  3. URI:URL URN
  4. HTTP协议:将超文本从一台主机传输到另一台主机的应用层协议

工作原理

请求/响应交互模型

  1. 浏览器分析URL
  2. 请求DNS解析
  3. DNS将IP地址返回浏览器
  4. 浏览器与服务器建立TCP连接
  5. 浏览器请求文档:get
  6. 服务器发出响应
  7. 释放TCP连接

HTTP方法

序号 方法 描述
1 GET 请求指定的页面信息,并返回实体主体
2 HEAD 类似GET,返回信息中只有报头
3 POST 向指定资源提交数据进行处理请求,例如上传文件、提交表单
4 PUT 从客户端向服务器传送的数据取代指定的文档内容
5 DELETE 请求服务器删除指定页面
6 CONNECT 预留给能够将连接改为管道方式的代理服务器
7 OPTIONS 允许客户端查看服务器的性能
8 TRACE 回显服务器收到的请求,主要用于测试或诊断
9 PATCH 对PUT方法的补充,用来对已知资源进行局部更新

状态码

  1. 作用:web服务器告诉客户端发生了什么事
  2. 分类
状态码 定义范围 分类
1xx 100~101 信息提示
2xx 200~206 请求成功
3xx 300~307 请求重定向
4xx 400~417 客户端错误
5xx 500~505 服务端错误
[root@nginx ~]# http www.sujx.net
HTTP/1.1 200 OK # 协议类型 状态可达
Connection: keep-alive # 连接
Content-Encoding: gzip # 使用gzip压缩
Content-MD5: BTwHjr+y4KyVqNv75IKHmg== # 文件校验
Content-Type: text/html # 文本类型
Date: Mon, 04 Dec 2023 10:29:39 GMT # 当前GMT时间
Last-Modified: Fri, 01 Dec 2023 07:05:58 GMT
Server: AliyunOSS # 对端服务器
Transfer-Encoding: chunked #传输编码形式 CDN相关
Vary: Accept-Encoding
x-oss-hash-crc64ecma: 188757334745851243
x-oss-object-type: Normal
x-oss-request-id: 656DAA133FF6C33232D7085F
x-oss-server-time: 15
x-oss-storage-class: Standard

WEB服务原理

工作模式

模式 内容 优点 缺点
多进程 每个请求分配一个进程 进程独立,稳定性好 资源占用高
多线程 一个进程生成多个线程来响应请求 轻量级,效率高 稳定性差,某个线程异常会造成整个进程故障
异步 响应请求非同步进行 性能最高 稳定性最差

响应过程

nginx

  1. 客户发起请求到服务器网卡
  2. 服务器网卡接受到请求后交给内核处理
  3. 内核根据请求对应的套接字,将请求交给工作在用户空间中web服务器进程
  4. web服务器进程根据用户请求,向内核进程系统调用,申请获取响应资源,如用户访问 index.html
  5. 内核发现web服务器进程请求的是一个存放在硬盘上的资源,因此通过驱动程序连接磁盘
  6. 内核调度磁盘,获取需要的资源
  7. 内核将资源存放在自己的缓冲区中,并通知web服务器进程
  8. web服务器进程通过系统调用获取资源,并将其复制到进程自己的缓冲区中
  9. web服务器进程形成响应,通过系统调用再次发给内核以响应用户请求
  10. 内核将响应发送至网卡
  11. 网卡发送响应给用户

I/O理论模型

根据前述WEB服务器的响应过程,整个HTTP交互有两次 I/O调用:

  1. 客户端请求的网络I/O
  2. web服务器请求页面的磁盘I/O

用户空间中的web服务器进程向内核进行系统调用申请IO,内核将资源从IO调度到内核的buffer中(wait阶段),内核还需将数据从内核buffer中复制(copy阶段)到web服务器进程所在的用户空间,才算完成一次IO调度。这几个阶段都是需要时间的。根据wait和copy阶段处理等待的机制不同,可将I/O动作分为如下五种模式:

nginx

  1. 阻塞:IO函数没有接收完数据或没有得到结果之前,函数不会返回;
  2. 非阻塞:调用IO函数,函数立即返回,等到处理结果后通过select通知调用者
  3. IO复用(select、poll):一个IO,两次调用、两次返回,同时阻塞多个IO操作
  4. 事件驱动的 IO:信号处理函数中处理IO操作
  5. 异步 IO:调用发出后,不等待结果,实际处理完成之后再通知调用者

I/O模型的实现

主要实现方式有以下几种:

  • select
  • poll
  • epoll
  • kqueue
  • /dev/poll
  • iocp

select、poll、epoll 是 Linux 实现的,kqueue是FreeBSD实现的,/dev/poll 是SUN公司在Solaris中实现的,iocp是windows实现的。

select、poll 对应第三种(IO复用)模型

iocp对应第五种(异步IO)模型

epoll、kqueue、/dev/poll 其实也同select属于同一种模型,只是更高级些,可以看做有了第4种(信号驱动IO)模型的某些特性,如callback机制。

主要服务器的工作模式

高并发的方法

  • 基于线程,即一个进程生成多个线程,每个线程响应用户的每个请求。
  • 基于事件的模型,一个进程处理多个请求,并且通过epoll机制来通知用户请求完成。
  • 基于磁盘的AIO(异步I/O)
  • 支持mmap内存映射,mmap传统的web服务器,进行页面输入时,都是将磁盘的页面先输入到内核缓存中,再由内核缓存中复制一份到web服务器上,mmap机制就是让内核缓存与磁盘进行映射,web服务器,直接复制页面内容即可。不需要先把磁盘的上的页面先输入到内核缓存去。

Apache

apache有三种工作模式,分别为prefork、worker、event。

  • prefork:多进程,每个请求用一个进程响应,这个过程会用到select机制来通知。
  • worker:多线程,一个进程可以生成多个线程,每个线程响应一个请求,但通知机制还是select不过可以接受更多的请求。
  • event:基于异步I/O模型,一个进程或线程,每个进程或线程响应多个用户请求,它是基于事件驱动(也就是epoll机制)实现的。

Nginx

受启发于多种操作系统设计中基于“事件”的高级处理机制,nginx采用了模块化、事件驱动、文件异步、单线程及非阻塞的架构,并大量采用了多路复用及事件通知机制。在nginx中,连接请求由为数不多的几个仅包含一个线程的进程worker以高效的回环(run-loop)机制进行处理,而每个worker可以并行处理数千个的并发连接及请求。 Nginx会按需同时运行多个进程:一个主进程(master)和几个工作进程(worker),配置了缓存时还会有缓存加载器进程(cache loader)和缓存管理器进程(cache manager)等。所有进程均是仅含有一个线程,并主要通过“共享内存”的机制实现进程间通信。主进程以root用户身份运行,而worker、 cache loader和cache manager均应以非特权用户身份运行。

nginx

Nginx入门

安装部署

# 使用dnf安装rpm包
[root@nginx ~]# dnf install -y nginx
[root@nginx ~]# nginx -v
nginx version: nginx/1.14.1
[root@nginx ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx ~]# firewall-cmd --permanent --add-service={http,https}
success
[root@nginx ~]# firewall-cmd --reload
success
[root@nginx ~]# http 127.0.0.1
HTTP/1.1 200 OK
Accept-Ranges: bytes
Connection: keep-alive
Content-Length: 3854
Content-Type: text/html
Date: Mon, 04 Dec 2023 11:17:34 GMT
ETag: "607d55d7-f0e"
Last-Modified: Mon, 19 Apr 2021 10:05:11 GMT
Server: nginx/1.14.1
# Nginx配置文件
[root@nginx ~]# rpm -ql nginx
/etc/logrotate.d/nginx # 日志切割配置
/etc/nginx/fastcgi.conf # 代理相关文件
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/koi-utf # 编码转换文件
/etc/nginx/koi-win
/etc/nginx/win-utf
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf # 主配置文件
/etc/nginx/nginx.conf.default
/usr/bin/nginx-upgrade
/usr/lib/.build-id
/usr/lib/.build-id/9e
/usr/lib/.build-id/9e/a0c37fbdedec3eef7cd27798ef3182e9c0790d
/usr/lib/systemd/system/nginx.service # 服务配置文件
/usr/lib64/nginx/modules # 模块
/usr/sbin/nginx # 程序主进程
/usr/share/doc/nginx # 文档
/usr/share/doc/nginx/CHANGES
/usr/share/doc/nginx/README
/usr/share/doc/nginx/README.dynamic
/usr/share/licenses/nginx
/usr/share/licenses/nginx/LICENSE
/usr/share/man/man3/nginx.3pm.gz # 帮助文件
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html # 默认站点404页面
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html # 默认站点主页面
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim
/var/lib/nginx # 日志目录
/var/lib/nginx/tmp
/var/log/nginx

站点搭建

# 编写配置文件,设置game.contoso.com为站点访问域名
[root@nginx ~]# cat /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name game.contoso.com;

location / {
root /var/www/game;
index index.htm;
}
}
# 下载吃豆人源码
[root@nginx ~]# git clone https://github.com/platzhersh/pacman-canvas.git
[root@nginx ~]# mkdir -p /var/www/code
[root@nginx ~]# cp ~/pacman-canvas/* /var/www/code -rf
[root@nginx ~]# chown -R nginx:nginx /var/www/code/
[root@nginx ~]# systemctl reload nginx

nginx

日志管理

  1. 默认日志定义语法格式
# 日志格式设置
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 /var/log/nginx/access.log main;
  1. 自定义日志
server {
listen 80;
server_name game.contoso.com;
access_log /var/log/nginx/game.contoso.com.log main; # 自定义日志位置和日志等级

location / {
root /var/www/game;
index index.htm;
}

location /favicon.ico {
access_log off; # 关闭日志记录
return 200;
}
}
  1. 日志切割
[root@nginx ~]# cat /etc/logrotate.d/nginx 
/var/log/nginx/*log {
create 0664 nginx root # 日志文件权限
daily # 每天切割日志
rotate 10 # 保存10天日志
missingok # 日志丢失忽略
notifempty # 不切割空日志
compress # 日志文件压缩
sharedscripts
postrotate # 日志切割命令
/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}
[root@nginx nginx]# cat game.contoso.com.log
192.168.10.1 - - [04/Dec/2023:20:16:53 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0" "-"
192.168.10.1 - - [04/Dec/2023:20:16:53 +0800] "GET /style.css HTTP/1.1" 304 0 "http://game.contoso.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0" "-"

基础功能

目录索引

# 列出整个目录浏览下载
[root@nginx ~]# cat /etc/nginx/conf.d/pub.conf
server {
listen 80;
server_name pub.contoso.com;

location / {
root /var/www/pub;
charset utf-8,gbk;
autoindex on;
autoindex_localtime on;
autoindex_exact_size off;
}
}

状态监控

使用http_stub_status_module模块

# 使用status作为监控地址
[root@nginx ~]# cat /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name game.contoso.com;
access_log /var/log/nginx/game.contoso.com.log main;

location / {
root /var/www/game;
index index.htm;
}

location /status {
stub_status;
}
}
[root@nginx ~]# curl -l game.contoso.com/status
Active connections: 7 # 当前活动的链接
server accepts handled requests
31 31 160
Reading: 0 Writing: 1 Waiting: 6
# 请求 响应 等待

配置SSL

cat /etc/nginx/conf.d/game.conf 
# 配置301跳转
server {
listen 80 ;
return 301 https://$host$request_uri;
server_name game.contoso.com;
}
# 配置HTTPS站点
server {
listen 443;
server_name game.contoso.com;
# 证书设置
ssl on;
ssl_certificate /var/www/game/ssl/contoso.com.crt;
ssl_certificate_key /var/www/game/ssl/contoso.com.key;

access_log /var/log/nginx/game.contoso.com.log main;

location / {
root /var/www/game;
index index.htm;
}

location /status {
stub_status;
}
}

访问控制

基于IP

使用http_access_module

server {
listen 443;
server_name game.contoso.com;

ssl on;
ssl_certificate /var/www/game/ssl/contoso.com.crt;
ssl_certificate_key /var/www/game/ssl/contoso.com.key;

access_log /var/log/nginx/game.contoso.com.log main;

location / {
root /var/www/game;
index index.htm;
}

location /status {
stub_status;
deny 192.168.10.254; # 阻止固定IP
allow 192.168.10.0/24; # 允许网段
allow 127.10.0.1;
deny all; # 阻止其他
}
}

基于用户

使用http_auth_basic_module

# 使用htpasswd 命令创建用户和密码
[root@nginx ~]# dnf install httpd-tools
[root@nginx ~]# htpasswd -b -c /etc/nginx/auth_conf contoso passwd
Adding password for user contoso
# 添加访问控制
server {
listen 80 ;
return 301 https://$host$request_uri;
server_name game.contoso.com;
}

server {
listen 443;
server_name game.contoso.com;

ssl on;
ssl_certificate /var/www/game/ssl/contoso.com.crt;
ssl_certificate_key /var/www/game/ssl/contoso.com.key;

access_log /var/log/nginx/game.contoso.com.log main;

location / {
root /var/www/game;
index index.htm;
}

location /favicon.ico {
access_log off;
return 200;
}

location /status {
stub_status;
deny 192.168.10.254;
allow 192.168.10.0/24;
allow 127.10.0.1;
deny all;

auth_basic "Auth access need INPUT your PASSWD!";
auth_basic_user_file auth_conf;
}
}

nginx

基于连接

使用ngx_http_limit_conn_module模块。注意,只有大文件才有效果,小文件不容易出现并发的现象。

# 限制每个地址只能有一个连接数
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;

server {
listen 80 ;
return 301 https://$host$request_uri;
server_name game.contoso.com;
}

server {
listen 443;
server_name game.contoso.com;

ssl on;
ssl_certificate /var/www/game/ssl/contoso.com.crt;
ssl_certificate_key /var/www/game/ssl/contoso.com.key;

access_log /var/log/nginx/game.contoso.com.log main;

location / {
limit_conn conn_zone 1; #实际配置在站点根目录
root /var/www/game;
index index.htm;
}

location /favicon.ico {
access_log off;
return 200;
}

location /status {
stub_status;
deny 192.168.10.254;
allow 192.168.10.0/24;
allow 127.10.0.1;
deny all;

auth_basic "Auth access need INPUT your PASSWD!";
auth_basic_user_file auth_conf;
}
}
基于请求
# 设定每秒连接数为1个IP
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;

server {
listen 80 ;
return 301 https://$host$request_uri;
server_name game.contoso.com;
}

server {
listen 443;
server_name game.contoso.com;

ssl on;
ssl_certificate /var/www/game/ssl/contoso.com.crt;
ssl_certificate_key /var/www/game/ssl/contoso.com.key;

access_log /var/log/nginx/game.contoso.com.log main;

location / {
limit_conn conn_zone 1;
# req_zone 每秒连接数为1个,最大等待上限为3个,其他则返回503
limit_req zone=req_zone burst=3 nodelay;
root /var/www/game;
index index.htm;
}
}

# 测试
[root@infra ~]# ab -n 100 -c 2 https://game.contoso.com/
192.168.10.254 - - [05/Dec/2023:22:06:21 +0800] "GET / HTTP/1.0" 200 10268 "-" "ApacheBench/2.3" "-"
192.168.10.254 - - [05/Dec/2023:22:06:21 +0800] "GET / HTTP/1.0" 200 10268 "-" "ApacheBench/2.3" "-"
192.168.10.254 - - [05/Dec/2023:22:06:21 +0800] "GET / HTTP/1.0" 200 10268 "-" "ApacheBench/2.3" "-"
192.168.10.254 - - [05/Dec/2023:22:06:21 +0800] "GET / HTTP/1.0" 200 10268 "-" "ApacheBench/2.3" "-"
192.168.10.254 - - [05/Dec/2023:22:06:21 +0800] "GET / HTTP/1.0" 503 213 "-" "ApacheBench/2.3" "-"
192.168.10.254 - - [05/Dec/2023:22:06:21 +0800] "GET / HTTP/1.0" 503 213 "-" "ApacheBench/2.3" "-"
192.168.10.254 - - [05/Dec/2023:22:06:21 +0800] "GET / HTTP/1.0" 503 213 "-" "ApacheBench/2.3" "-"
192.168.10.254 - - [05/Dec/2023:22:06:21 +0800] "GET / HTTP/1.0" 503 213 "-" "ApacheBench/2.3" "-"
配置路径优先级
优先级 匹配符 匹配规则
1 = 精确匹配
2 ^~ 以某个字符串开头
3 ~ 区分大小写的正则匹配
4 ~* 不区分大小写的正则匹配
5 !~ 区分大小写不匹配的正则
6 !~* 不区分大小写不匹配的正则
7 / 通用匹配,任何请求都会匹配到
# 直接转发给后端应用服务器,也可以是一个静态首页
location = / {
proxy_pass http://tomcat:8080/index
}

# 处理静态文件请求
# 有两种配置模式,目录匹配或后缀匹配
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}

连接PHP

# 安装PHP
[root@nginx ~]# dnf module list php
Last metadata expiration check: 1:12:31 ago on Tue 05 Dec 2023 09:12:43 PM CST.
AlmaLinux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
php 8.0 common [d], devel, minimal PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
[root@nginx ~]# dnf module -y enable php:8.0
[root@nginx ~]# dnf module -y install php:8.0/common
[root@nginx ~]# php -v
PHP 8.0.30 (cli) (built: Aug 3 2023 17:13:08) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.30, Copyright (c) Zend Technologies

# 代理Fastcgi
[root@nginx ~]# systemctl enable --now php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/info.php

nginx

部署HTTP2站点

HTTP/2是HTTP协议的2.0版本,该协议通过多路复用、请求优化、HTTP头压缩等功能提升网络传输速度、优化用户体验。Nginx通过ngx_http_v2_module实现对HTTP/2协议的支持。

server {
listen 80 ;
return 301 https://$host$request_uri;
server_name game.contoso.com;
}

server {
listen 443 ssl http2; # 开启http2协议支持
server_name game.contoso.com;

ssl_certificate /var/www/game/ssl/contoso.com.crt;
ssl_certificate_key /var/www/game/ssl/contoso.com.key;

access_log /var/log/nginx/game.contoso.com.log main;

root /var/www/game;
location / {
http2_push /style.css; # 主动推送css文件
index index.htm;
}
}

代理服务

服务种类

正向代理

在内部网络建立外部站点的镜像,让内部访问外部服务

# 默认nginx7层正向代理无法使用HTTPS,需要单独编译ngx_http_proxy_connect_module 模块
[root@nginx conf.d]# cat connect.conf
server {
listen 8080; # 配置代理端口

resolver 114.114.114.114; # 配置访问dns
location /{
proxy_pass http://$host$request_uri;
}
}

# 普通访问百度
[root@nginx conf.d]# curl -I http://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Wed, 06 Dec 2023 11:19:51 GMT
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
Server: bfe/1.0.8.18 # 百度服务器

# 使用正向代理访问baidu
[root@nginx conf.d]# curl -I -x 127.0.0.1:8080 http://www.baidu.com
HTTP/1.1 200 OK
Server: nginx/1.14.1 # 服务器为本地服务器
Date: Wed, 06 Dec 2023 11:19:34 GMT
Content-Type: text/html
Content-Length: 277
Connection: keep-alive
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache

反向代理

将内部服务发布到外部站点,让外部访问内部服务

# 设置两个后端站点
[root@infra ~]# curl 192.168.10.235
<h1>This is WebSite 192.168.10.235</h1>
[root@infra ~]# curl 192.168.10.236
<h1>This is WebSite 192.168.10.236</h1>
# 增加反代站点
[root@nginx conf.d]# cat proxy.conf
server {
listen 80;
server_name proxy.contoso.com;

location / {
proxy_pass http://192.168.10.235;
}
}
# 访问proxy.contoso.com获得235站点主页
[root@nginx conf.d]# curl -L proxy.contoso.com
<h1>This is WebSite 192.168.10.235</h1>

负载均衡

# 配置反向代理参数
[root@nginx conf.d]# cat ../proxy_params
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 1600k;
proxy_buffers 4 3200k;
proxy_busy_buffers_size 6400k;
proxy_temp_file_write_size 6400k;
proxy_max_temp_file_size 128m;

# 配置负载均衡
[root@nginx conf.d]# cat proxy.conf
upstream node {
# ip_hash; # 同一IP访问一个后端服务器,不能和weight同时使用
# url_hash; # 相同url访问相同后端服务器
least_conn # 最少连接数分配,那个连接数少分配那个
server 192.168.10.236 weight=5; # weight越大,优先级越高
server 192.168.10.235 weight=10;
}

server {
listen 80;
server_name proxy.contoso.com;

location / {
proxy_pass http://node;
include proxy_params;
}
}
# 检查结果
[root@nginx conf.d]# curl proxy.contoso.com
<h1>This is WebSite 192.168.10.235</h1>
[root@nginx conf.d]# curl proxy.contoso.com
<h1>This is WebSite 192.168.10.236</h1>
[root@nginx conf.d]# curl proxy.contoso.com
<h1>This is WebSite 192.168.10.235</h1>
[root@nginx conf.d]# curl proxy.contoso.com
<h1>This is WebSite 192.168.10.235</h1>
[root@nginx conf.d]# curl proxy.contoso.com
<h1>This is WebSite 192.168.10.236</h1>

性能优化

# 通用优化配置
#将nginx进程设置为普通用户,为了安全考虑
user nginx;
group nginx;

#当前启动的worker进程,官方建议是与系统核心数一致
worker_processes auto;
#CPU亲和配置为自动分配绑定
worker_cpu_affinity auto;

#日志配置成warn
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

#针对 nginx 句柄的文件限制
worker_rlimit_nofile 35535;

#事件模型
events {
#使用epoll内核模型
use epoll;
#每一个进程可以处理多少个连接,如果是多核可以将连接数调高 worker_processes * 1024
worker_connections 10240;
}

http {
# 发送给客户端头部Content-Type的类型
include /etc/nginx/mime.types;
default_type application/octet-stream;

charset utf-8; #设置字符集

#设置日志输出格式,根据自己的情况设置
log_format main '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$args" "$request_uri"';
# 访问日志
access_log /var/log/nginx/access.log main;

# 屏蔽nginx版本号
server_tokens off;

# 文件高效传输,静态文件服务器建议打开
sendfile on;
tcp_nopush on;

# 文件实时传输,动态资源服务器建议打开
tcp_nodelay on;
# keepalived 配置
keepalive_timeout 65;

# 文件上传大小限制
client_max_body_size 2000m;
# Gzip 压缩
gzip on; #文件压缩默认可以打开
gzip_disable "MSIE [1-6]\.";
gzip_http_version 1.1;
gzip_comp_level 3; # 三级压缩

# 虚拟主机
include /etc/nginx/conf.d/*.conf;
}

参考

  1. Nginx教程
  2. Nginx中文官方站培训