为了方便管理还是推荐写一个nginx服务的管理脚本
[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx ##编写service启动脚本 #!/bin/bash # chkconfig: - 99 20 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0 [root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx [root@nginx nginx-1.12.2]# chkconfig --add nginx 3.4.1暂时注释原有的nginx的代理服务配置 location / { root html; index index.html index.htm; #proxy_pass ; } 3.4.2修改默认的站点目录文件(显示页面)(声明nginx作为静态资源访问的请求处理端) [root@nginx html]# vim index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p><em>this is a static web page.</em></p> #em表示斜体 </body> </html>重启服务此时访问nginx服务器,获取的是:
3.4.3在tomcat1上写一个jsp的动态页面 #创建一个站点目录demo,编写一个jsp脚本 vim /usr/local/tomcat/webapps/demo/index.jsp <!DOCCTYPE html> <%@ page language=https://www.linuxidc.com/Linux/2020-04/"java" contentType=https://www.linuxidc.com/Linux/2020-04/"text/html; charset=UTF-8" pageEncoding=https://www.linuxidc.com/Linux/2020-04/"UTF-8"%> <%@ page import=https://www.linuxidc.com/Linux/2020-04/"java.util.Date" %> <%@ page import=https://www.linuxidc.com/Linux/2020-04/"java.text.SimpleDateFormat" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd"> <html> <head> <meta http-equiv=https://www.linuxidc.com/Linux/2020-04/"Content-Type" content=https://www.linuxidc.com/Linux/2020-04/"text/html; charset=UTF-8"> <title>动态页面</title> </head> <body> <div>动态页面1</div> </body> </html> 更改server.xml <Host name=https://www.linuxidc.com/Linux/2020-04/"localhost" appBase=https://www.linuxidc.com/Linux/2020-04/"webapps" unpackWARs=https://www.linuxidc.com/Linux/2020-04/"true" autoDeploy=https://www.linuxidc.com/Linux/2020-04/"true"> <Context docBase=https://www.linuxidc.com/Linux/2020-04/"/usr/local/tomcat/webapps/demo" path=https://www.linuxidc.com/Linux/2020-04/"" reloadable=https://www.linuxidc.com/Linux/2020-04/"false"> </Context> <!--<Context docBase=https://www.linuxidc.com/Linux/2020-04/"/web/webapp1" path=https://www.linuxidc.com/Linux/2020-04/"" reloadable=https://www.linuxidc.com/Linux/2020-04/"false"> </Context> -->那么此时在nginx服务器上需要location对访问的jsp文件进行ip跳转访问的配置:
location ~.*.jsp$ { ##匹配jsp页面跳转代理服务器池 proxy_pass ; proxy_set_header Host $host; } 3.4.4测试验证 3.5对于同一web页面实现动态访问tomcat,静态资源从nginx上获取使用一张图片作为jsp文件,其中包含一张jpg格式的图片从nginx服务器上获取
具体配置如下