唯有天下一统,方能开的万世太平
Nginx的主要作用:
- 反向代理
- 负载均衡
1.Ubuntu Server 安装 Nginx
1.升级apt
sudo apt update
2.安装nginx
sudo apt install nginx
3.查看版本
nginx -V
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
2.Nginx 配置
Nginx的配置文件位于/etc/nginx/
中
配置信息在nginx.conf
中,默认的内容为:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 将其他配置文件引入
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
配置内容分类:
Directive | Description | Contains Directive |
---|---|---|
main | nginx 在运行时与具体业务功能(比如 http 服务或者 email 服务代理)无关的一些参数,比如工作进程数,运行的身份等。 | user, worker_processes, error_log, events, http, mail |
http | 与提供 http 服务相关的一些配置参数。例如:是否使用 keepalive 啊,是否使用 gzip 进行压缩等。 | server |
server | http 服务上支持若干虚拟主机。每个虚拟主机一个对应的 server 配置项,配置项里面包含该虚拟主机相关的配置。在提供 mail 服务的代理时,也可以建立若干 server. 每个 server 通过监听的地址来区分。 | listen, server_name, access_log, location, protocol, proxy, smtp_auth, xclient |
location | http 服务中,某些特定的 URL 对应的一系列配置项。 | index, root |
实现 email 相关的 SMTP/IMAP/POP3 代理时,共享的一些配置项(因为可能实现多个代理,工作在多个监听地址上)。 | server, http, imap_capabilities | |
include | 以便增强配置文件的可读性,使得部分配置文件可以重新使用。 | - |
valid_referers | 用来校验Http请求头Referer是否有效。 | - |
try_files | 用在server部分,不过最常见的还是用在location部分,它会按照给定的参数顺序进行尝试,第一个被匹配到的将会被使用。 | - |
if | 当在location块中使用if指令,在某些情况下它并不按照预期运行,一般来说避免使用if指令。 | - |
Nginx配置文件主要分成四部分:
- main(全局设置)
- server(主机设置)
- upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)
- location(URL匹配特定位置后的设置)
SpringBoot + nginx配置
- 将SpringBoot项目打包成JAR文件
sdurunner1.jar
- 将文件上传到多个Linux服务器上
- 在其中一个服务器中启动Nginx
- 然后将所有服务器上的jar文件运行
- 之后便可以通过访问Nginx的监听端口访问
其中Nginx配置文件内容 /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
#负载均衡,将接收到的请求分配到以下服务器,并指定每个服务器的权重
upstream sdurunner {
server 192.168.101.100:8443 weight=1;
server 192.168.101.101:8443 weght=1;
}
#服务器监听
server {
listen 8999; #监听端口
server_name 192.168.101.100; #监听地址
location / {
root html;
proxy_pass http://sdurunner; #监听转发地址,转发到上面的负载均衡中服务器地址
index index.html index.htm;
}
}
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
启动Nginx
$ sudo nginx -c /etc/nginx/nginx.conf
重启Nginx
$ systemctl restart nginx
若启动时端口被占用,杀死80端口的进程
$ sudo fuser -k 80/tcp
3.请求处理流程
Springboot的web请求是由SpringMVC进行处理的。
Spring MVC是Spring的一个模块,是一个web框架。通过Dispatcher Servlet, ModelAndView 和 View Resolver,开发web应用变得很容易。解决的问题领域是网站应用程序或者服务开发——URL路由、Session、模板引擎、静态Web资源等等。
在没有设置nginx之前,HTTP请求直接进入Controller层,进行匹配。
在application.properties
中设置启动占用端口
应用启动后,可在本地通过127.0.0.1:8443
进行访问,在同一个局域网内,可用过IP地址+端口进行访问(此项目的两个IP地址为192.168.101.100
,192.168.101.101
,并且在192.168.101.100
)。
通过此地址,再经过Controller层的过滤,送入到不同的Service进行处理
引入Nginx之后
所以访问192.168.101.100:8999/api/commit/deletecommit?pid=1&nickname=sss
会将该请求分配到 192.168.101.100
,192.168.101.101
中的其中一个
数据处理完成后,再通过来时的路线原路返回到Nginx再回到客户端