Nginx超快速入门
文章目录
- 前言
- 一、Nginx
- 二、功能
- 1.Web服务器
- 2.负载均衡
- 3.正向代理
- 4.反向代理
- 5.动静分离
- 三、windows下载
- 1.启动
- 2.关闭
- 3.常用命令
- 4.代码解析
- 1.全局配置
- 2.每个进程的最大连接数
- 3.HTTP核心模块
- 4.Server服务
- 5.反向代理服务
- 6.websocket服务
- 总结
前言
Nginx(发音为“engine-x”)是一款 高性能的开源 Web 服务器,同时也可作为 反向代理、负载均衡器、HTTP 缓存 和 邮件代理服务器。它的设计注重高并发、低内存消耗和高稳定性,广泛用于现代互联网架构。
一、Nginx
- 定义:Nginx 是高性能的 HTTP 和反向代理的web服务器。
二、功能
1.Web服务器
Nginx可以作为静态页面的web服务器,如果写过go项目,有一个static的资源包,那里面就是静态资源,静态网页,如html、css、js等。支持CGI协议的动态语言,比如PHP,不支持Java,Java需要与tomcat配合完成。
- CGI协议:通用网关接口,是一种早期的Web服务器与外部程序交互的协议,用于动态生成网页的内容。它定义了Web服务器(如apache、nginx)如何将HTTP请求传递给外部程序(python、php等),并接收其返回的响应数据。
- 为什么java不能和nginx直接集成使用?
- 因为java是编译型语言,运行在自己的jvm中,主要支持http、ajp协议,不需要外部的FastCGI协议,比如python和php都需要和FastCGI使用,而Java本身有自带的服务器tomcat,所以nginx无法直接处理这样一个例外。
2.负载均衡
负载均衡是一种分配网络或计算负载的技术,目的是将请求或任务均匀分发到多个服务器、服务或资源上,以提高系统的性能、可靠性和可扩展性。
- 说白了就是将多个请求和任务均匀从一个服务器分发到多个服务器进行处理,这样构成一个集群来处理。
- 联想到集群的概念,集群是指 将多台计算机(服务器)通过软件或网络连接起来,协同工作,对外表现为一个统一的计算资源。
- Nginx提供的负载均衡策略有两种:内置策略和扩展策略。内置策略是轮询(加权轮询、Ip hash),扩展策略是我们自定义的。
3.正向代理
正向代理是一种通过代理服务器访问服务器的过程,客户端向服务器发起请求并返回响应。客户端先向代理服务器进行发送请求,而代理服务器再将请求发到服务器,此时代理服务器保护了客户端的ip,服务器这个时候只知道代理服务器的ip。
4.反向代理
反向代理是一种代表服务器接收客户端请求的过程,客户端发送请求给反向代理服务器(比如Nginx),反向代理服务器接收请求,转发给后端服务器,将结果返回给反向代理服务器,最终返回给客户端,这个时候,客户端不知道后端服务器的ip地址,这样就隐藏了后端服务器地址。
5.动静分离
动静分离是将静态资源(如HTML/CSS/JS/图片)和动态请求(如API/数据库查询)分开处理 的架构设计,核心目的是 提升网站性能、降低服务器压力。静态资源和动态请求分别打到不同的服务器进行处理。
三、windows下载
直接下载解压就可以了。Nginx下载
1.启动
跟redis差不多,需要进入nginx.exe所在的目录,打开cmd,输入nginx.exe进行运行,双击运行也可以,双击只会一闪而过。我这里权限不够所以有报错,正常的不会有报错信息。
默认是80端口,可以打开nginx.config进行配置。
server{
listen: 80;
}
2.关闭
关闭窗口不能让nginx停止,要使用命令。
要在nginx.exe所在目录下打开cmd。
用后者可以保存数据的完整性。
nginx -s stop 或 nginx -s quit
3.常用命令
nginx.exe 启动
nginx -s stop 停止
nginx -s quit 安全退出
nginx -s reload 重新加载配置文件 修改了配置文件后必须执行噢
grep nginx 查看进程
4.代码解析
#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;map $http_upgrade $connection_upgrade{default upgrade;'' close;}upstream webservers{server 127.0.0.1:8080 weight=90 ;#server 127.0.0.1:8088 weight=10 ;}server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html/sky;index index.html index.htm;}#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;}# 反向代理,处理管理端发送的请求location /api/ {proxy_pass http://localhost:8080/admin/;#proxy_pass http://webservers/admin/;}# 反向代理,处理用户端发送的请求location /user/ {proxy_pass http://webservers/user/;}# WebSocketlocation /ws/ {proxy_pass http://webservers/ws/;proxy_http_version 1.1;proxy_read_timeout 3600s;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "$connection_upgrade";}# 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_certificate cert.pem;# ssl_certificate_key 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;# }#}}
1.全局配置
#user nobody; # 默认以nobody用户运行,注释状态,实际使用为默认用户
worker_processes 1; # 工作进程数 建议设置为cpu核心数#error_log logs/error.log; # 错误日志路径位置,注释状态,使用默认
#error_log logs/error.log notice; # 可指定日志级别,notice、info、debug等
#error_log logs/error.log info; # 同上#pid logs/nginx.pid; # 进程id文件路径
2.每个进程的最大连接数
events {worker_connections 1024; # 每个worker进程的最大连接数 1024个
}
3.HTTP核心模块
http {include mime.types; # 引入MIME类型定义文件default_type application/octet-stream; # 默认响应类型(二进制流)sendfile on; # 启用高效文件传输模式keepalive_timeout 65; # 客户端保持连接超时时间(秒)# WebSocket支持配置map $http_upgrade $connection_upgrade {default upgrade;'' close;}# 定义上游服务器组(负载均衡)upstream webservers {server 127.0.0.1:8080 weight=90; # 权重90%#server 127.0.0.1:8088 weight=10; # 备用服务器(注释状态)}
4.Server服务
server {listen 80; # 监听80端口server_name localhost; # 域名(本地测试用)# 静态资源服务location / {root html/sky; # 静态文件根目录(html/sky)index index.html index.htm; # 默认索引文件}# 错误页面配置error_page 500 502 503 504 /50x.html;location = /50x.html {root html; # 50x错误页存放目录}
5.反向代理服务
# 管理端API代理location /api/ {proxy_pass http://localhost:8080/admin/; # 转发到本地8080端口#proxy_pass http://webservers/admin/; # 注释的负载均衡方案}# 用户端API代理location /user/ {proxy_pass http://webservers/user/; # 通过upstream组转发}
6.websocket服务
location /ws/ {proxy_pass http://webservers/ws/; # 代理WebSocket请求proxy_http_version 1.1; # 强制HTTP/1.1协议proxy_read_timeout 3600s; # 连接超时时间(1小时)proxy_set_header Upgrade $http_upgrade; # 升级协议头proxy_set_header Connection "$connection_upgrade"; # 连接类型}
总结
Nginx是一个高性能的静态资源服务器,它具有网关、负载均衡、动静分离等功能,非常高效的提升了放问速度。