Stage 3 day08 nginx

Half bump 2020-11-09 10:50:38
stage day08 day nginx

  1. File upload optimization


1.1 url Optimize

explain : If you need to access the server through a network virtual path . It should be implemented in the following configuration .

  1. Local disk path : D:JT-SOFTimages20200930a.jpg
  2. Network virtual path : http://image.jt.com20200930a.jpg

1.2 edit pro The configuration file

 Insert picture description here

1.3 Complete the dynamic assignment of attributes

`package com.jt.service;
import com.jt.vo.ImageVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@PropertySource("classpath:/properties/") // The container dynamically loads the specified configuration file
public class FileServiceImpl implements FileService{
// Since the value of an attribute may change later , So we should get the attribute data dynamically . utilize pro The configuration file
private String rootDirPath; // = "D:/JT-SOFT/images";
private String urlPath; // = "";
//1.2 Prepare a collection of pictures Contains all types of pictures .
private static Set<String> imageTypeSet;
static {
imageTypeSet = new HashSet<>();
* Perfect calibration process
* 1. Check whether it is a picture
* 2. Verify whether it is a malicious program
* 3. Prevent too many files , Sub directory storage .
* 4. Prevent duplicate names of files
* 5. File upload .
* @param uploadFile
* @return
public ImageVO upload(MultipartFile uploadFile) {
//0. Prevent extra space So first do the processing of the space
//1. Check image type jpg|png|gif..JPG|PNG....
//1.1 Get the name of the current picture And then intercept the type . abc.jpg
String fileName = uploadFile.getOriginalFilename();
int index = fileName.lastIndexOf(".");
String fileType = fileName.substring(index);
// Convert data to lowercase
fileType = fileType.toLowerCase();
//1.3 Determine whether the picture type is correct .
// The image type doesn't match
//2. Verify whether it is a malicious program According to width / Height to judge
try {
//2.1 utilize an instrument API object Read byte information . Get the image object type
BufferedImage bufferedImage =;
//2.2 Check for width and height
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if(width==0 || height==0){
//3. Sub directory storage yyyy/MM/dd Separate
//3.1 Set the time according to the specified format Convert to string .
String dateDir = new SimpleDateFormat("/yyyy/MM/dd/")
.format(new Date());
//3.2 The directory object where the file is stored
String fileDirPath = rootDirPath + dateDir;
File dirFile = new File(fileDirPath);
//3.3 Create a dynamic directory
//4. Prevent duplicate names of files uuid.jpg Dynamic splicing
//4.1 Dynamic generation uuid Realize file name splicing name . suffix
String uuid =
UUID.randomUUID().toString().replace("-", "");
String realFileName = uuid + fileType;
//5 File upload
//5.1 The actual path of the splicing file dir/ File name .
String realFilePath = fileDirPath + realFileName;
//5.2 Encapsulated object Upload
File realFile = new File(realFilePath);
// File upload success !!! http://image.jt.com20200930a.jpg
String url = urlPath + dateDir + realFileName;
return ImageVO.success(url,width,height);
} catch (IOException e) {

2 Reverse proxy mechanism

2.1 Why do I need a reverse proxy

demand : When the file upload is complete , What the business returns to the page is the virtual address path
url Address :
Real picture address : file:///D:/JT-SOFT/image/2020/09/30/d534bed912c748b0ac979ee40222490a.jpg
problem : How to let users through url visit Find a picture of the real disk address .

2.2 Reverse proxy mechanism

2.2.1 Introduction to reverse agent

The reverse proxy server is located between the user and the target server , But for users , The reverse proxy server is equivalent to the target server , namely Users can obtain the resources of the target server by directly accessing the reverse proxy server . meanwhile , user You don't need to know the address of the target server , There is no need to make any settings on the client side . A reverse proxy server is usually used as Web Speed up , Use reverse proxy as Web Front end server to reduce network and server load , Improve access efficiency .
Generalization :
1. Located in the user ( client )- Between servers .
2. User access to reverse proxy server , Think it's real server information .
3. Users have no idea who the real server information is .
4. The general mechanism of proxy server to protect the real information , So it's also called Server side agents .
 Insert picture description here

2.3 Forward proxy mechanism

2.3.1 Demand introduction

1. Broadband : Telecom operators account number / password Can only be used by one machine .
2. Router : Created a local area network inside the home The device in LAN can communicate with the outside world through the function of router .

2.3.2 Introduce to agent

Forward agency , It means a location between the client and the original server (origin server) Server between , To get content from the original server , The client sends a request to the agent and specifies the destination ( Original server ), The agent then forwards the request to the original server and returns the obtained content to the client . Client can use forward proxy .
summary :
1. The forward proxy is between the client and the server
2. Before the client initiates the request The address of the target server is determined .
3. The server doesn't know which client is accessing me , Thought it was just router access .
4. Forward proxy protects the customer's information , So it's also called Client agent
 Insert picture description here

2.4 Nginx

2.4.1 Nginx Introduce

Nginx (engine x) Is a high-performance HTTP And reverse proxy web The server , It also provides IMAP/POP3/SMTP service .Nginx It was by igor · Mr. Seshoyev was the second most visited city in Russia Site ( ru :Рамблер) Developed , The first public version 0.1.0 Published on 2004 year 10 month 4 Japan .
It takes the source code as a class BSD Release in the form of license , Because of its stability 、 Rich feature set 、 Known for sample profiles and low system resource consumption .2011 year 6 month 1 Japan ,nginx 1.0.4 Release .
Nginx It's a lightweight model Web The server / Reverse proxy server and email (IMAP/POP3) proxy server , stay BSD-like Issue under agreement . It is characterized by less memory , Strong concurrency , in fact nginx The concurrency ability of is better in the same type of web server , Used in mainland China nginx Website users have : Baidu 、 JD.COM 、 Sina 、 NetEase 、 tencent 、 Taobao, etc. .

characteristic :
1. Less memory No more than 2M tomcat The server takes up about 600M
2. Strong concurrency 3-5 Ten thousand times / second tomcat The server is about 150-220 Between

2.4.2 Nginx Install and use

 Insert picture description here
matters needing attention :

  1. Don't put nginx Put it in C Disk and system directory Pay attention to Chinese paths and spaces .
  2. nginx The server starts up very fast , The window will flash back Just start it once
  3. nginx Start up will take up 80 port .
  4. nginx The command must run in nginx.exe Execute... In the directory .

2.4.2 Nginx command

1). Start command start nginx
2). Restart command nginx -s reload
3). Stop the order nginx -s stop

2.4.3 Nginx Server startup item description

 Insert picture description here

2.4.4 About nginx Reverse proxy description

 `http {
# A reverse proxy is a server
server {
# monitor 80 port
listen 80;
# Monitored domain name Domain name cannot be repeated .
server_name localhost;
# The reverse proxy action performed / Block all paths
location / {
# root keyword The agent is a directory
root html;
# Default jump page
index index.html index.htm;

2.5 Nginx Realize image echo

2.5.1 NGINX To configure

`# Image server agent
server {
listen 80;
location / {
# Turn to the catalog
root D:/JT-SOFT/images;

2.5.2 modify hosts file

1.HOSTS Description of the document :
 Insert picture description here
2.HOSTS The location of the file
 Insert picture description here
3). Run as super administrator
 Insert picture description here

 Insert picture description here

`# Jingtao configuration
# On the left IP Address Write the domain name on the right Use a space between them
# Realization nginx Of
#Bug Sometimes the letters may be lost when using the software .`

3.nginx Realization tomcat Cluster deployment

3.1 Project deployment

 Insert picture description here

3.2 Server reverse proxy

 `# Configure Jingtao background management server
# localhost:8091 The server
server {
listen 80;
location / {
# Mapping server
proxy_pass http://localhost:8091;

modify nignx After the server , restart nginx
 Insert picture description here

3.3 Get the current server port number dynamically

public class PortController {
// from spring Dynamic access to the server port number
private Integer port;
public String getPort(){
return " Current server access port number :"+port;

3.4 Project package

 Insert picture description here
 Insert picture description here
And then from the project target Get dynamically from the directory jar Package file Prepare for cluster deployment .
 Insert picture description here

3.4 Project release order

matters needing attention : The current command is executed Will occupy dos Command window Print console information . When dos The command window closes The server stops .
sign out dos Command window : ctrl + c

`java: java -jar 8081.war`
* 1

 Insert picture description here

3.4 nginx Load balancing implementation

3.4.1 Polling mechanism

explain : In the order of the configuration files Access the server in turn .

`# Configure Jingtao background management server
# localhost:8091 The server
server {
listen 80;
location / {
# Mapping server
#proxy_pass http://localhost:8091;
proxy_pass http://jtWindows;
# To configure tomcat Server cluster 1. Polling strategy
upstream jtWindows {
#server Represents the server address

3.4.2 Weight mechanism

explain : According to the weight setting , Let the server with better performance handle more requests .

`# To configure tomcat Server cluster 1. Polling strategy 2. Weight strategy
upstream jtWindows {
#server Represents the server address
server weight=8;
server weight=2;

3.4.3 IPHASH Strategy ( understand )

Because some data is bound to the server , Then you have to ask the user to access the specified server , Use IPHASH Strategy

`# To configure tomcat Server cluster 1. Polling strategy 2. Weight strategy 3.iphash Strategy
upstream jtWindows {
#server Represents the server address
server weight=8;
server weight=2;


1. install VMware Virtual machine program
 Insert picture description here

2. Check the network card settings
If there is no network card Then change it to another one vmwar Version installed …
 Insert picture description here

  1. start-up Linux System

 Insert picture description here
 Insert picture description here
 Insert picture description here

Problem description :
Get into BIOS In the system Turn on the virtualization settings . Motherboard system Turn it on F1/F2…
 Insert picture description here

本文为[Half bump]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple