Nginx 的三大功能
1.HTTP服务器
Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML、图片)通过HTTP协议展现给客户端。
2.反向代理服务器
Nginx也是反向代理服务器。
说反向代理之前先说一下正向代理,正向代理相信很多大陆同胞都在这片神奇的土地上用过了。就是访问国外网被墙了,然后找个代理服务,通过该服务器访问国外网站,这个是正向代理。
反向代理是 客户端访问代理服务器,但是代理服务器没有用户需要的资源,然后代理服务器偷偷访问应用服务器,获取资源返回给用户,用户不知道代理服务器是访问了应用服务器,代理服务器也隐藏了应用服务器的url。(反向代理的典型用途是将 防火墙后面的服务器提供给Internet用户访问)
3.负载均衡
Nginx可以通过反向代理来实现负载均衡。
二、Nginx 安装
2.1 CentOS 7 安装 Nginx
2.1.1.添加Nginx到YUM源
添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令:
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.2.2.安装Nginx
在你的CentOS 7 服务器中使用yum命令从Nginx源服务器中获取来安装Nginx:
sudo yum install -y nginx
Nginx将完成安装在你的CentOS 7 服务器中。
2.2.3. Nginx启动
nginx -c nginx.conf
启动后就可以访问 域名或者本机IP 如果出现下图,说明启动成功。
2.2.4. Nginx 停止
//查询出nginx的pid
ps -ef|grep nginx
//通过kill 命令杀死 nginx
kill pid
注意图中nginx的 pid 为 10497
2.2.5. Nginx配置信息
网站文件存放默认目录
/usr/share/nginx/html
网站默认站点配置
/etc/nginx/conf.d/default.conf
自定义Nginx站点配置文件存放目录
/etc/nginx/conf.d/
Nginx全局配置
/etc/nginx/nginx.conf
2.2 docker 安装 Nginx
2.2.1下载镜像,
docker pull nginx:1.9
2.2.2 启动容器,
docker run -d -p 8080:80 nginx:1.9
把容器内的nginx的80端口,映射到当前服务器的8080端口,假设当前服务器的ip是192.168.0.100,浏览器输入http://192.168.0.100:8080/,就可以看到nginx已启动,
三、Nginx 使用
关于静态资源服务器的使用这里就不做说明了。
3.1 反向代理
小例子:实现访问本机 ip 然后代理 我的博地址 ,也就是在浏览器输入本机地址,然后跳转到我的博客。
3.1.1 本地安装nginx
如果是本地安装的nginx 的话需要修改 /etc/nginx/conf.d/default.conf 配置文件
注释原有的 location 然后替换为新的location
//注释原有的
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
//新添加的
location / {
proxy_pass http://blog.csdn.net/u012373815?viewmode=list;
}
然后重启nginx ,访问本机ip 就会代理到“http://blog.csdn.net/u012373815?viewmode=list“ 我的博客地址。
3.1.2 Docker 安装
如果是docker 安装的话,需要本地随意目录下新建default.conf 文件,内容如下:
server {
listen 80;
server_name localhost;
<span class="hljs-variable"><span class="hljs-comment">#charset koi8</span><span class="hljs-attribute"><span class="hljs-comment">-r;</span>
<span class="hljs-variable"><span class="hljs-comment">#access_log /</span><span class="hljs-built_in"><span class="hljs-comment">var/</span><span class="hljs-keyword"><span class="hljs-comment">log/nginx/</span><span class="hljs-keyword"><span class="hljs-comment">log/host</span><span class="hljs-built_in"><span class="hljs-comment">.access</span><span class="hljs-built_in"><span class="hljs-comment">.</span><span class="hljs-keyword"><span class="hljs-comment">log main;</span>
<span class="hljs-attribute">location</span> <span class="hljs-subst">/ {
<span class="hljs-attribute">proxy_pass</span> http:<span class="hljs-comment">//blog.csdn.net/u012373815?viewmode=list;
}
<span class="hljs-attribute">error_page</span> <span class="hljs-number"><span class="hljs-number">500</span> <span class="hljs-number"><span class="hljs-number">502</span> <span class="hljs-number"><span class="hljs-number">503</span> <span class="hljs-number"><span class="hljs-number">504</span> /<span class="hljs-number">50x<span class="hljs-built_in">.html;
<span class="hljs-attribute">location</span> <span class="hljs-subst">= /<span class="hljs-number">50x<span class="hljs-built_in">.html {
<span class="hljs-attribute">root</span> /usr/share/nginx/html;
}
<span class="hljs-variable"><span class="hljs-comment">#charset koi8</span><span class="hljs-attribute"><span class="hljs-comment">-r;</span>
<span class="hljs-variable"><span class="hljs-comment">#access_log /</span><span class="hljs-built_in"><span class="hljs-comment">var/</span><span class="hljs-keyword"><span class="hljs-comment">log/nginx/</span><span class="hljs-keyword"><span class="hljs-comment">log/host</span><span class="hljs-built_in"><span class="hljs-comment">.access</span><span class="hljs-built_in"><span class="hljs-comment">.</span><span class="hljs-keyword"><span class="hljs-comment">log main;</span>
<span class="hljs-attribute">location</span> <span class="hljs-subst">/ {
<span class="hljs-attribute">proxy_pass</span> http:<span class="hljs-comment">//blog.csdn.net/u012373815?viewmode=list;
<span class="hljs-attribute">error_page</span> <span class="hljs-number"><span class="hljs-number">500</span> <span class="hljs-number"><span class="hljs-number">502</span> <span class="hljs-number"><span class="hljs-number">503</span> <span class="hljs-number"><span class="hljs-number">504</span> /<span class="hljs-number">50x<span class="hljs-built_in">.html;
<span class="hljs-attribute">location</span> <span class="hljs-subst">= /<span class="hljs-number">50x<span class="hljs-built_in">.html {
<span class="hljs-attribute">root</span> /usr/share/nginx/html;
}
}
然后重新启动容器,将default.conf 文件映射到docker容器内。
启动命令如下:
docker run -p 8080:80 --name myNginx -v /本地路径/default.conf:/etc/nginx/conf.d/default.conf -d nginx:1.9
容器启动后 ,访问本机ip 就会代理到“http://blog.csdn.net/u012373815?viewmode=list“ 我的博客地址。
3.2 负载均衡
上面的配置只实现了反向代理没有实现传说中的负载均衡。所有的请求就都被反向代理到 我的博客地址去了。这样我们反向代理的功能是实现了,可是就能代理到一台服务器上哪有什么负载均衡呀?这就要用到 nginx 的 upstream 模块了。
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server backend4.example.com;
}
location / {
proxy_pass http://backend;
}
我们在 upstream 中指定了一组机器,并将这个组命名为 backend,这样在 proxypass 中只要将请求转移到 backend 这个 upstream 中我们就实现了在四台机器的反向代理加负载均衡。其中的 iphash指明了我们均衡的方式是按照用户的 ip 地址进行分配。
要让配置生效,我们不必重启 nginx 只需要 reload 配置即可。
负载均衡配置示例
假设这样一个应用场景:将应用部署在 192.168.1.11:80、192.168.1.12:80、192.168.1.13:80 三台 linux 环境的服务器上。网站域名叫 www.helloworld.com,公网 IP 为 192.168.1.11。在公网 IP 所在的服务器上部署 nginx,对所有请求做负载均衡处理。
nginx.conf 配置如下:
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /var/log/nginx/access.log;
<span class="hljs-comment"><span class="hljs-comment">#设定负载均衡的服务器列表</span>
<span class="hljs-attribute"><span class="hljs-attribute">upstream</span> load_balance_server {
<span class="hljs-comment"><span class="hljs-comment">#weigth参数表示权值,权值越高被分配到的几率越大</span>
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.11:80</span> weight=<span class="hljs-number"><span class="hljs-number">5</span>;
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.12:80</span> weight=<span class="hljs-number"><span class="hljs-number">1</span>;
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.13:80</span> weight=<span class="hljs-number"><span class="hljs-number">6</span>;
}
#HTTP服务器
server {
#侦听80端口
listen 80;
<span class="hljs-comment"><span class="hljs-comment">#定义使用www.xx.com访问</span>
<span class="hljs-attribute"><span class="hljs-attribute">server_name</span> www.helloworld.com;
<span class="hljs-comment"><span class="hljs-comment">#对所有请求进行负载均衡请求</span>
<span class="hljs-attribute"><span class="hljs-attribute">location</span> / {
<span class="hljs-attribute"><span class="hljs-attribute">root</span> /root; <span class="hljs-comment"><span class="hljs-comment">#定义服务器的默认网站根目录位置</span>
<span class="hljs-attribute"><span class="hljs-attribute">index</span> index.html index.htm; <span class="hljs-comment"><span class="hljs-comment">#定义首页索引文件的名称</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_pass</span> http://load_balance_server ;<span class="hljs-comment"><span class="hljs-comment">#请求转向load_balance_server 定义的服务器列表</span>
<span class="hljs-comment"><span class="hljs-comment">#以下是一些反向代理的配置(可选择性配置)</span>
<span class="hljs-comment"><span class="hljs-comment">#proxy_redirect off;</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> Host <span class="hljs-variable"><span class="hljs-variable">$host</span>;
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> X-Real-IP <span class="hljs-variable"><span class="hljs-variable">$remote_addr</span>;
<span class="hljs-comment"><span class="hljs-comment">#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> X-Forwarded-For <span class="hljs-variable"><span class="hljs-variable">$remote_addr</span>;
<span class="hljs-attribute"><span class="hljs-attribute">proxy_connect_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#nginx跟后端服务器连接超时时间(代理连接超时)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_send_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#后端服务器数据回传时间(代理发送超时)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_read_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#连接成功后,后端服务器响应时间(代理接收超时)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_buffer_size</span> <span class="hljs-number"><span class="hljs-number">4k</span>; <span class="hljs-comment"><span class="hljs-comment">#设置代理服务器(nginx)保存用户头信息的缓冲区大小</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_buffers</span> <span class="hljs-number"><span class="hljs-number">4</span> <span class="hljs-number"><span class="hljs-number">32k</span>; <span class="hljs-comment"><span class="hljs-comment">#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_busy_buffers_size</span> <span class="hljs-number"><span class="hljs-number">64k</span>; <span class="hljs-comment"><span class="hljs-comment">#高负荷下缓冲大小(proxy_buffers*2)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_temp_file_write_size</span> <span class="hljs-number"><span class="hljs-number">64k</span>; <span class="hljs-comment"><span class="hljs-comment">#设定缓存文件夹大小,大于这个值,将从upstream服务器传</span>
<span class="hljs-attribute"><span class="hljs-attribute">client_max_body_size</span> <span class="hljs-number"><span class="hljs-number">10m</span>; <span class="hljs-comment"><span class="hljs-comment">#允许客户端请求的最大单文件字节数</span>
<span class="hljs-attribute"><span class="hljs-attribute">client_body_buffer_size</span> <span class="hljs-number"><span class="hljs-number">128k</span>; <span class="hljs-comment"><span class="hljs-comment">#缓冲区代理缓冲用户端请求的最大字节数</span>
}
}
<span class="hljs-comment"><span class="hljs-comment">#设定负载均衡的服务器列表</span>
<span class="hljs-attribute"><span class="hljs-attribute">upstream</span> load_balance_server {
<span class="hljs-comment"><span class="hljs-comment">#weigth参数表示权值,权值越高被分配到的几率越大</span>
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.11:80</span> weight=<span class="hljs-number"><span class="hljs-number">5</span>;
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.12:80</span> weight=<span class="hljs-number"><span class="hljs-number">1</span>;
<span class="hljs-attribute"><span class="hljs-attribute">server</span> <span class="hljs-number"><span class="hljs-number">192.168.1.13:80</span> weight=<span class="hljs-number"><span class="hljs-number">6</span>;
}
<span class="hljs-comment"><span class="hljs-comment">#定义使用www.xx.com访问</span>
<span class="hljs-attribute"><span class="hljs-attribute">server_name</span> www.helloworld.com;
<span class="hljs-comment"><span class="hljs-comment">#对所有请求进行负载均衡请求</span>
<span class="hljs-attribute"><span class="hljs-attribute">location</span> / {
<span class="hljs-attribute"><span class="hljs-attribute">root</span> /root; <span class="hljs-comment"><span class="hljs-comment">#定义服务器的默认网站根目录位置</span>
<span class="hljs-attribute"><span class="hljs-attribute">index</span> index.html index.htm; <span class="hljs-comment"><span class="hljs-comment">#定义首页索引文件的名称</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_pass</span> http://load_balance_server ;<span class="hljs-comment"><span class="hljs-comment">#请求转向load_balance_server 定义的服务器列表</span>
<span class="hljs-comment"><span class="hljs-comment">#以下是一些反向代理的配置(可选择性配置)</span>
<span class="hljs-comment"><span class="hljs-comment">#proxy_redirect off;</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> Host <span class="hljs-variable"><span class="hljs-variable">$host</span>;
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> X-Real-IP <span class="hljs-variable"><span class="hljs-variable">$remote_addr</span>;
<span class="hljs-comment"><span class="hljs-comment">#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_set_header</span> X-Forwarded-For <span class="hljs-variable"><span class="hljs-variable">$remote_addr</span>;
<span class="hljs-attribute"><span class="hljs-attribute">proxy_connect_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#nginx跟后端服务器连接超时时间(代理连接超时)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_send_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#后端服务器数据回传时间(代理发送超时)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_read_timeout</span> <span class="hljs-number"><span class="hljs-number">90</span>; <span class="hljs-comment"><span class="hljs-comment">#连接成功后,后端服务器响应时间(代理接收超时)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_buffer_size</span> <span class="hljs-number"><span class="hljs-number">4k</span>; <span class="hljs-comment"><span class="hljs-comment">#设置代理服务器(nginx)保存用户头信息的缓冲区大小</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_buffers</span> <span class="hljs-number"><span class="hljs-number">4</span> <span class="hljs-number"><span class="hljs-number">32k</span>; <span class="hljs-comment"><span class="hljs-comment">#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_busy_buffers_size</span> <span class="hljs-number"><span class="hljs-number">64k</span>; <span class="hljs-comment"><span class="hljs-comment">#高负荷下缓冲大小(proxy_buffers*2)</span>
<span class="hljs-attribute"><span class="hljs-attribute">proxy_temp_file_write_size</span> <span class="hljs-number"><span class="hljs-number">64k</span>; <span class="hljs-comment"><span class="hljs-comment">#设定缓存文件夹大小,大于这个值,将从upstream服务器传</span>
<span class="hljs-attribute"><span class="hljs-attribute">client_max_body_size</span> <span class="hljs-number"><span class="hljs-number">10m</span>; <span class="hljs-comment"><span class="hljs-comment">#允许客户端请求的最大单文件字节数</span>
<span class="hljs-attribute"><span class="hljs-attribute">client_body_buffer_size</span> <span class="hljs-number"><span class="hljs-number">128k</span>; <span class="hljs-comment"><span class="hljs-comment">#缓冲区代理缓冲用户端请求的最大字节数</span>
}
}
四、nginx配置location总结及rewrite规则写法
4.1 正则
.
: 匹配除换行符以外的任意字符?
: 重复0次或1次+
: 重复1次或更多次*
: 重复0次或更多次\d
:匹配数字^
: 匹配字符串的开始$
: 匹配字符串的介绍{n}
: 重复n次{n,}
: 重复n次或更多次[c]
: 匹配单个字符c[a-z]
: 匹配a-z小写字母的任意一个~ 区分大小写匹配
~* 不区分大小写匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配
^ 以什么开头的匹配
$ 以什么结尾的匹配
-
小括号
()
之间匹配的内容,可以在后面通过$1
来引用,$2
表示的是前面第二个()
里的内容。正则里面容易让人困惑的是\
转义特殊字符。
4.2 常用变量
$args
: #这个变量等于请求行中的参数,同$query_string
$content_length
: 请求头中的Content-length字段。$content_type
: 请求头中的Content-Type字段。$document_root
: 当前请求在root指令中指定的值。$host
: 请求主机头字段,否则为服务器名称。$http_user_agent
: 客户端agent信息$http_cookie
: 客户端cookie信息$limit_rate
: 这个变量可以限制连接速率。$request_method
: 客户端请求的动作,通常为GET或POST。$remote_addr
: 客户端的IP地址。$remote_port
: 客户端的端口。$remote_user
: 已经经过Auth Basic Module验证的用户名。$request_filename
: 当前请求的文件路径,由root或alias指令与URI请求生成。$scheme
: HTTP方法(如http,https)。$server_protocol
: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。$server_addr
: 服务器地址,在完成一次系统调用后可以确定这个值。$server_name
: 服务器名称。$server_port
: 请求到达服务器的端口号。$request_uri
: 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。$uri
: 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。$document_uri
: 与$uri相同。
4.3 flag标志位
last
: 相当于Apache的[L]标记,表示完成rewritebreak
: 停止执行当前虚拟主机的后续rewrite指令集redirect
: 返回302临时重定向,地址栏会显示跳转后的地址permanent
: 返回301永久重定向,地址栏会显示跳转后的地址
#路径重写配置Demo
location /demo/test/ {
#以/demo/test路径开始
#原始路径: http://127.0.0.1:8080/demo/test/1.html 重写后:http://127.0.0.1:8888/demo/test2/1.html
rewrite ^/demo/test/(.*)$ http://127.0.0.1:8888/demo/test2/$1 break;
<span class="hljs-comment"><span class="hljs-comment">#以html文件结尾</span>
<span class="hljs-comment"><span class="hljs-comment">#原始路径: http://127.0.0.1:8080/demo/test/1.html 重写后:http://127.0.0.1:8888/test/demo/test/1.html</span>
<span class="hljs-comment"><span class="hljs-comment"># 此种方式配置会改变浏览器地址,引发跨域请求的问题</span>
<span class="hljs-comment"><span class="hljs-comment">#rewrite ^/(.*\.html)$ http://127.0.0.1:8888/test/$1 break;</span>
<span class="hljs-comment"><span class="hljs-comment"># 此种方式,结合proxy_pass 重写后则不会改变浏览器地址,不存在跨域问题</span>
<span class="hljs-comment"><span class="hljs-comment">#rewrite ^/(.*\.html)$ /test/$1 break;</span>
<span class="hljs-comment"><span class="hljs-comment">#proxy_pass http://127.0.0.1:8888;</span>
<span class="hljs-comment"><span class="hljs-comment">#以html文件结尾</span>
<span class="hljs-comment"><span class="hljs-comment">#原始路径: http://127.0.0.1:8080/demo/test/1.html 重写后:http://127.0.0.1:8888/test/demo/test/1.html</span>
<span class="hljs-comment"><span class="hljs-comment"># 此种方式配置会改变浏览器地址,引发跨域请求的问题</span>
<span class="hljs-comment"><span class="hljs-comment">#rewrite ^/(.*\.html)$ http://127.0.0.1:8888/test/$1 break;</span>
<span class="hljs-comment"><span class="hljs-comment"># 此种方式,结合proxy_pass 重写后则不会改变浏览器地址,不存在跨域问题</span>
<span class="hljs-comment"><span class="hljs-comment">#rewrite ^/(.*\.html)$ /test/$1 break;</span>
<span class="hljs-comment"><span class="hljs-comment">#proxy_pass http://127.0.0.1:8888;</span>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!