一、安装 进入官网下载nginx.tar.gz
https://nginx.org/en/download.html
稳定版本(2023.06.15)
https://nginx.org/download/nginx-1.24.0.tar.gz
安装nginx相关依赖
cd /usr/srcwget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz tar -xvf pcre-8.37.tar.gz cd pcre-8.37.tar.gz/./configure make && make install pcre-config --version
安装openssl 和 zlib(安装方式2:yum安装)
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
rz tar -xvf nginx-1.24.0.tar.gz ./configure make && make install cd /usr/local/nginx/sbin./nginx ps -ef | grep nginx /usr/local/nginx/conf nginx.conf firewall-cmd --list-all firewall-cmd --add-service=http --permanent sudo firewall-cmd --add-port=80/tcp --permanent sudo firewall-cmd --remove-port=80/tcp --permanent firewall-cmd --reload
二、nginx常用命令 前提 :使用nginx命令需要进入nginx的目录
# 重启nginx.conf配置 ./nginx -s reload
三、nginx.conf配置文件 配置文件地址:/usr/local/nginx/conf/nginx.conf
配置文件组成(三部分):
全局块(配置文件开始到events块之间的内容):
nginx整体运行的配置
worker_processes值越大处理并发请求就越多
events块:
主要影响nginx服务器与用户的网络连接
worker_connections为配置nginx支持的最大用户连接数
http块:
http全局块配置指令:文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限等
server块:
虚拟主机相关配置,每个http块可以包含多个server块
每个server块分为全局server块,可同时包含多个location块
#user nobody; # worker_processes值越大处理并发请求就越多 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 { # nginx支持的最大用户连接数 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 / { # 网站首页地址:/html/dist下的index文件 root /html/dist; 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配置——反向代理 下载并启动tomcat
#rz 导入apache-tomcat-8.5.90.tar.gz # 解压Tomcat tar -xvf apache-tomcat-8.5.90.tar.gz # 进入解压目录bin cd apache-tomcat-8.5.90/bin # 启动tomcat ./startup.bat # 进入解压目录log cd apache-tomcat-8.5.90/logs # 动态查看日志 tail -f catalina.out
4.1 反向代理案例1 现在要实现一个功能,windows下访问www.bamboo.com跳转到192.168.49.10反向代理到127.0.0.1:8080
www.bamboo.com:配置windows中host文件中的域名和映射的ip地址
浏览器解析域名先通过本地host文件查找,找不到再去网络中的DNS域名解析器找服务器
C:/Windows/System32/drivers/etc/hosts
注意不要开代理,梯子之类的
192.168.49.10:80为nginx对外开放的80端口
127.0.0.1:8080端口为tomcat开放的8080端口
nginx.conf配置
# server片段 server { listen 80; # 当请求到192.168.49.10连接此server server_name 192.168.49.10; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; # 当访问/路径时反向代理到http://127.0.0.1:8080 proxy_pass http://127.0.0.1:8080; index index.html index.htm; }
4.2 反向代理案例2 在案例1的基础上更改,nginx端口9001,tomcat服务器两个,8080和8081
server { listen 9001; server_name 192.168.49.10; location ~ /edu/ { proxy_pass http://127.0.0.1:8080; } location ~ /vod/ { proxy_pass http://127.0.0.1:8081; }
*注:原始访问地址192.168.49.10:9001/edu会转发到http://127.0.0.1:8080/edu
4.3 location说明 location [ = | ~ | ~* | ^~] uri { }
=:严格匹配(不用)
~:表示uri包含正则表达式,区分大小写
~*:表示uri包含正则表达式,不区分大小写
^~:用于不含正则表达式的uri前(最优先执行),找到匹配度最高的location后立即使用此location处理请求,不再使用location块中的正则uri和请求字符串做匹配
五、nginx配置——负载均衡
nginx.conf中位于http全局块中,在server块前增加
upstream myserver { server 192.168.49.10:8080; server 192.168.49.10:8081; }
server中监听listen为80的server_name修改为确切端口号192.168.49.10
server_name 192.168.49.10;
location / 中修改proxy_pass为上面的upstream的名称
location / { # 负载均衡 proxy_pass http://myserver; root html; index index.html index.htm; }
效果 :每次刷新页面都会切换反向代理的端口,实现相同路径不同实现的目的,分摊压力
5.1 负载均衡分配策略
轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,则会被踢出。
weight
weight代表权,默认1,权重越高被分配的客户端越多
upstream server_pool { server 192.168.49.10:8080 weight=5; server 192.168.49.10:8081 weight=10; }
ip_hash
每个请求按访问ip的hash结果分配,每个访客固定访问一个后端服务器,可以解决session问题。
upstream server_pool { ip_hash; server 192.168.49.10:8080; server 192.168.49.10:8081; }
fair(第三方)
按后端服务器的响应时间来分配请求,相应时间短的优先分配
upstream server_pool { server 192.168.49.10:8080; server 192.168.49.10:8081; fair; }
六、nginx配置——动静分离 动态请求和静态请求分离
# 静态网页目录 /data/www # 静态图片目录 /data/image
server { listen 80; server_name 192.168.49.10; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; proxy_pass http://myserver; index index.html index.htm; } # 只有输入192.168.49.10/www/才会生效 location /www/ { root /data/; index index.html index.htm; } # 只有输入192.168.49.10/image/才会生效 location /image/ { root /data/; # 列出/data/image/目录中的内容 autoindex on; }
七、高可用集群 nginx与Tomcat都可能会宕机
nginx宕机 :请求无法使用
八、解决Vue直接通过路径访问报错404问题 location / { root /html/dist; try_files $uri $uri/ /index.html; index index.html index.htm; }