軟件介紹
- NSSM 簡介
NSSM 全稱為:the Non-Sucking Service Manager褒链。
從名稱上來看疑苔,功能應(yīng)該跟Forever類似,會自動幫你重啟兵迅,保證你的進程不會掛掉薪贫,NSSM 所采用的方式是把你的Nodejs應(yīng)用安裝為一個windows service,并開機自動運行扯夭;如果需要重啟應(yīng)用交洗,那么重啟對應(yīng)的windows服務(wù)就可以了。 - nginx 簡介
反向代理(Reverse Proxy)方式是指以代理服務(wù)器來接受internet上的連接請求隐圾,然后將請求轉(zhuǎn)發(fā)給內(nèi)部網(wǎng)絡(luò)上的服務(wù)器暇藏,并將從服務(wù)器上得到的結(jié)果返回給internet上請求連接的客戶端盐碱,此時代理服務(wù)器對外就表現(xiàn)為一個服務(wù)器瓮顽。
反向代理方式實際上就是一臺負(fù)責(zé)轉(zhuǎn)發(fā)的代理服務(wù)器围橡,貌似充當(dāng)了真正服務(wù)器的功能,但實際上并不是翁授,代理服務(wù)器只是充當(dāng)了轉(zhuǎn)發(fā)的作用晾咪,并且從真正的服務(wù)器那里取得返回的數(shù)據(jù)。這樣說谍倦,其實nginx完成的就是這樣的工作。我們讓nginx監(jiān)聽一個端口泪勒,譬如80端口,但實際上我們轉(zhuǎn)發(fā)給在8080端口的tomcat圆存,由它來處理真正的請求曹洽,當(dāng)請求完成后,tomcat返回辽剧,但數(shù)據(jù)此時沒直接返回税产,而是直接給nginx,由nginx進行返回撞羽,這里,我們會以為是nginx進行了處理邻奠,但實際上進行處理的是tomcat。
說到上面的方式碌宴,也許很多人又會想起來贰镣,這樣可以把靜態(tài)文件交由nginx來進行處理。對膳凝,很多用到nginx的地方都是作為靜態(tài)伺服器碑隆,這樣可以方便緩存那些靜態(tài)文件,比如CSS蹬音,JS上煤,html,htm等文件祟绊。
了解nginx配置項
[your_nginx_path]\conf\nginx.conf
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;
#}
}
這里面有幾個需要了解的地方:
- listen:表示當(dāng)前的代理服務(wù)器監(jiān)聽的端口楼入,默認(rèn)的是監(jiān)聽80端口哥捕。注意,如果我們配置了多個server嘉熊,這個listen要配置不一樣遥赚,不然就不能確定轉(zhuǎn)到哪里去了。
- server_name:表示監(jiān)聽到之后需要轉(zhuǎn)到哪里去阐肤,這時我們直接轉(zhuǎn)到本地凫佛,這時是直接到nginx文件夾內(nèi)。
- location:表示匹配的路徑孕惜,這時配置了/表示所有請求都被匹配到這里
- root:里面配置了root這時表示當(dāng)匹配這個請求的路徑時愧薛,將會在這個文件夾內(nèi)尋找相應(yīng)的文件,這里對我們之后的靜態(tài)文件伺服很有用衫画。
- index:當(dāng)沒有指定主頁時毫炉,默認(rèn)會選擇這個指定的文件,它可以有多個削罩,并按順序來加載瞄勾,如果第一個不存在,則找第二個弥激,依此類推进陡。
- error_page是代表錯誤的頁面。
配置實現(xiàn)
這里我們用nginx實現(xiàn)對tomcat的真實請求微服,也就是來自于所有來自外部的請求趾疚,進入nginx的80端口進行監(jiān)聽,如果靜態(tài)文件(js,css,image等等)以蕴,由nginx直接返回糙麦,如果是別的請求將由tomcat響應(yīng),tomcat只對nginx可見.
軟件準(zhǔn)備
nssm-2.24.zip
nginx-1.11.1.zip windows版本
加入到環(huán)境變量
path=F:\server\nssm-2.24\win64;F:\server\nginx-1.11.1;
準(zhǔn)備一個nginx配置文件舒裤,靜態(tài)文件由nginx直接處理喳资,動態(tài)文件由8080端口處理
F:\server\for-tomcat-nginx.conf
#user nobody;
worker_processes 1;
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;
# 轉(zhuǎn)發(fā)的服務(wù)器,upstream 為負(fù)載均衡做準(zhǔn)備
upstream tomcat_server{
server localhost:8080;
}
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root F:/data/wwwtest/test-nginx/target/classes/static;
charset UTF-8;
# 動態(tài)請求的轉(zhuǎn)發(fā)
location ~ .*.action$ {
proxy_pass http://tomcat_server;
proxy_set_header Host $host;
}
# 靜態(tài)請求直接讀取
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
expires 30d;
}
#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;
}
}
}
配置
命令行
nssm install
會彈出下面的配置界面:
開始服務(wù)
nssm start test-nginx
同時在開啟你的tomcat服務(wù)器
訪問/index.action
本文件的demo文件
spring boot+web
test-nginx\src\resources\templates\application.properties
spring.mvc.view.suffix=action
test-nginx\src\test\java\com\example\DemoApplicationTests.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@Controller
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/index")
public ModelAndView main(){
Map map=new HashMap();
map.put("message","你好腾供,tomLuo!");
return new ModelAndView("index",map);
}
}
test-nginx\src\main\resources\template\index.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test nginx</title>
</head>
<body>
<p>這是動態(tài)頁仆邓,來自實際的8080端口響應(yīng).</p>
<p>${message}</p>
</body>
</html>
test-nginx\src\main\resources\static\index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test nginx</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/index.action">訪問/index.action頁</a>
</div>
</div><!-- /.container -->
</nav>
<p>test nginx</p>
<img src="image/1.jpg"/>
<img src="image/2.jpg"/>
<script src="js/jquery-1.10.2.js"></script>
</body>
</html>