nginx
1.nginx基本概念
(1)nginx是什么,做什么事情。
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器 [13] ,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。支持高达五万个并发连接数。
(2)反向代理
nginx 不仅可以做反向代理,实现负载均衡,还可以做正向代理
正向代理:《用户访问谷歌》用户需要通过一个代理服务器(配置一个代理服务器)访问谷歌
反向代理:用户需要不需要配置代理服务器,由服务器选择对应的服务器
(3)负载均衡
将服务器的请求,分发到不同的服务器上
(4)动静分离
将服务器的动态资源和静态的资源交给不同的服务器解析。
2.nginx安装、常用命令和配置文件
(1)在linux系统中安装nginx
1.官网下载nginx
http://nginx.org/en/download.html
2、安装nginx相关的依赖
1.安装c
yum -y install gcc-c++
查看是否安装成功
gcc -v
2.安装pcre
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
cd pcre-8.37
./configure
查看是否安装成功
pcre-config --version
(3).安装其他的
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
3.安装nginx
解压
1 cd nginx
2 ./configure
3 make && make install
安装成功后会在/usr多出一个文件夹叫local/nginx,文件中sbin中有nginx的启动命令
启动nginx
cd /usr/local/nginx/sbin
./nginx
查看启动状态
ps -ef | grep nginx
浏览器访问http://192.168.72.6/
查看防火墙开放的端口号
firewall-cmd --list-all
开放端口号
sudo firewall-cmd --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
(2)nginx常用命令
使用nginx操作命令的前提条件:进入/usr/local/nginx/sbin
cd /usr/local/nginx/sbin
1.查看nginx的版本号
./nginx -v
2.启动nginx
./nginx
3.关闭nginx
./nginx -s stop
4.重启nginx
./nginx -s reload
(3)nginx配置文件
位置:./usr/local/nginx/conf/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;
}
#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_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;
# }
#}
}
nginx有三部分:
第一部份全局快
主要设置一些影响nginx服务器整体运行的配置指令
#user nobody;
worker_processes 1; #nginx处理并发的数量,值越大,处理的并发量越多
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
第二部分 events块
events {
worker_connections 1024;#用户连接数量
}
第三部分 http块
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; #监听80端口
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #路径
root html;
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;
}
# 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_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;
# }
#}
}
3.nginx配置实例 1-反向代理
1、实现效果
(1)打开浏览器,在浏览器输入www.123.com,跳转到Tomcat主页面
2、准备工作
(2)在linux中安装Tomcat默认端口号8080
解压Tomcat进入bin启动Tomcat:./startup.sh
(3)在windows浏览器访问Tomcat
第一步:
在Windows系统的C:\Windows\System32\drivers\etc下的hosts文件加上一个域名映射
192.168.72.6 www.123.com
第二步:
修改:nginx的配置文件
listen 80;
server_name 192.168.72.6;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://127.0.0.1:8080
index index.html index.htm;
}
.nginx配置实例 2-反向代理
效果:
访问 http://127.0.0.1:9001/vod/ 直接跳到127.0.0.1:8080
访问 http://127.0.0.1:9001/edu/ 直接跳到127.0.0.1:8081
2.准备工作
(1)准备两个Tomcat服务器
在8080的Tomcat的webapps文件夹下新建一个vod目录里面放一个a.html
在8080的Tomcat的webapps文件夹下新建一个edu目录里面放一个a.html
修改:nginx的配置文件
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 192.168.72.6;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://127.0.0.1:8080;
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;
}
server {
listen 9001;
server_name 192.168.72.6;
location ~ /edu {
proxy_pass http://127.0.0.1:8081;
}
location ~ /vod {
proxy_pass http://127.0.0.1:8080;
}
}
# 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
#
# 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;
# }
#}
}
4.nginx配置实例 2-负载均衡
(1)浏览器地址栏输入地址http://192.168.17.129/edu/a.html,负载均衡效果,平均到8080和8081
2.准备工作
(1)准备两台Tomcat服务器,8080和8081
(2)在两台Tomcat服务器webapps目录中,创建edu文件夹,用于测试
(3)在nginx配置文件中进行负载均衡
upstream myserver{
server 192.168.72.6:8080 weight=1;
server 192.168.72.6:8081 weight=1;
}
server_name 192.168.72.6;
location / {
root html;
proxy_pass http://myserver;
index index.html index.htm;
}
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;
upstream myserver{
server 192.168.72.6:8080 weight=1;
server 192.168.72.6:8081 weight=1;
}
server {
listen 80;
server_name 192.168.72.6;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://myserver;
index index.html index.htm;
}
....
}
负载均衡策略:
1.轮询(默认)
2.权重(weight)
upstream myserver{
server 192.168.72.6:8080 weight=1;
server 192.168.72.6:8081 weight=1;
}
权重最高的先访问
3.ip_hash
upstream myserver{
ip_hash;
server 192.168.72.6:8080;
server 192.168.72.6:8081;
}
用户第一次访问哪台服务器,会一直访问哪台服务器
4.fair(第三方)
upstream myserver{
server 192.168.72.6:8080;
server 192.168.72.6:8081;
fair;
}
根据服务器的响应时间分配
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)