实训—Nginx直接上手

326 阅读3分钟

Nginx

把打包好的jar包放进Linux里

image-20210122214400109

应用程序部署概述

1)后端应用程序:
	打包jar
	服务器:tomcat、weblogic、jboss(SpringBoot使用内置的Tomcat服务器)
2)前端应用程序:
	vue开发-->转换(build)-->(html、static[js、css、font])
	服务器: nginx
	
nginx服务器作用:
	1)作为前端应用程序运行的服务器
	2)应对高并发访问,实现负载均衡

1.后端应用程序打包部署

用maven进行打包 上传 bjmarket-0.0.1-SNAPSHOT.jar 到linux系统的/opt/data

1.1 后台运行jar

出现问题:

image-20210122222023679

解决:

原因可能是,你在处理某些问题的时候,不小心把 pom.xml 配置文件中的下面代码删除掉,然后打包执行,就出现了该问题

<build>
	<plugins>
        <!-- 加上这段代码 -->
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin> 
	</plugins>
</build>

重新打包上传后

[hadoop@hadoop01 data]$ nohup java -jar bjmarket-0.0.1-SNAPSHOT.jar &
[1] 8243
[hadoop@hadoop01 data]$ nohup: 忽略输入并把输出追加到'nohup.out'

【检测到jar进行运行】
[hadoop@hadoop01 data]$ jps
8243 jar
8253 Jps
[hadoop@hadoop01 data]$ 

【注意】
结尾: & (保证后台运行)

基于网页测试

image-20210122222508614

1.2停止后台运行的jar

[hadoop@hadoop01 data]$ jps
8243 jar
8253 Jps
[hadoop@hadoop01 data]$ kill -9 8243 #基于进程号结束进程 -9代表强制
[hadoop@hadoop01 data]$ jps
8311 Jps
[1]+  已杀死               nohup java -jar bjmarket-0.0.1-SNAPSHOT.jar
[hadoop@hadoop01 data]$ 

2.Nginx服务使用

此时的前端程序在本地也是能访问的(localhost改成远程主机的地址)。但是还是要部署在Nginx里

2.1 nginx概述

  • nginx高性能的web服务器
  • 反向代理服务器、负载均衡

测试版本

[hadoop@hadoop01 data]$ nginx -v
nginx version: nginx/1.16.1

修改被指文件nginx.conf

位置: /opt/model/nginx/conf

[hadoop@hadoop01 conf]$ vi nginx.conf
#显示行号: :set nu
#1)删掉34~39行   命令:6dd
34 upstream hitech {
35 ip_hash;
36 server 192.168.1.181:8081;
37 server 192.168.1.181:8082;
38 server 192.168.1.181:8083;
39 }
#2)删掉42~45行   4dd
42 location /pie {
43 proxy_pass http://hitech;
44
45 }

#3)改后的效果是
34 server {
35 	listen 8011;#端口号是8011
36	 server_name localhost;
37
38 #charset koi8-r;
39
40 #access_log logs/host.access.log main;
41
42
43 location / {
44 root html;
45 index index.html index.htm;
46 }
【最后保存 :wq】

nginx命令

启动nginx服务
[hadoop@hadoop01 conf]$ nginx #启动
[hadoop@hadoop01 conf]$ ps -ef | grep nginx #验证nginx是否运行  ps -ef查看当前正在运行的进程
hadoop    8450     1  0 22:40 ?        00:00:00 nginx: master process nginx
hadoop    8451  8450  0 22:40 ?        00:00:00 nginx: worker process
hadoop    8461  3220  0 22:40 pts/0    00:00:00 grep --color=auto nginx

基于网页验证nginx启动成功

[hadoop@hadoop01 conf]$ cd ..
[hadoop@hadoop01 nginx]$ ll
总用量 0
drwx------ 2 hadoop hadoop   6 7月  16 2020 client_body_temp
drwxrwxr-x 2 hadoop hadoop 333 1月  22 22:39 conf
drwx------ 2 hadoop hadoop   6 7月  16 2020 fastcgi_temp
drwxr-xr-x 3 hadoop hadoop  54 7月  16 2020 html
drwxrwxr-x 2 hadoop hadoop  58 1月  22 22:40 logs
drwx------ 2 hadoop hadoop   6 7月  16 2020 proxy_temp
drwxrwxr-x 2 hadoop hadoop  19 7月  16 2020 sbin
drwx------ 2 hadoop hadoop   6 7月  16 2020 scgi_temp
drwx------ 2 hadoop hadoop   6 7月  16 2020 uwsgi_temp
[hadoop@hadoop01 nginx]$ cd html
[hadoop@hadoop01 html]$ ll
总用量 8
-rw-r--r-- 1 hadoop hadoop 494 7月  16 2020 50x.html
-rw-r--r-- 1 hadoop hadoop 512 7月  16 2020 index.html
drwxrwxr-x 5 hadoop hadoop  40 7月  16 2020 static
[hadoop@hadoop01 html]$ 

里面有一个我们写好的index.html页面

http://192.168.0.104:8011/

image-20210122224335431

重新加载配置信息

将前端程序:piesite (将static和pie.html放到/opt/model/nginx/html/目录)

static

  • js

pie.html

别忘了改这一行

image-20210122225222536 image-20210122224849627
[hadoop@hadoop01 html]$ nginx -s reload

访问部署好的前端程序

http://192.168.1.104:8011/pie.html

注意:要保证部署好的后端正在运行

image-20210122225509623