1.nginx+tomcat
yum install java -y
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gz
mkdir /soft
tar xf apache-tomcat-9.0.34.tar.gz -C /soft/
ln -s /soft/apache-tomcat-9.0.34/ /soft/tomcat #创建软连接方便升级
/soft/tomcat/bin/startup.sh #启动tomcat
#注意tomcat默认运行在8080端口
vim /etc/nginx/conf.d/tomcat.oldxu.conf
-------------/etc/nginx/conf.d/tomcat.oldxu.conf-------------------
server {
listen 80;
server_name tomcat.oldxu.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* \.(png|gif)$ {
root /code/images;
expire 30d;
}
}
-------------/etc/nginx/conf.d/tomcat.oldxu.conf结束----------------
mv /soft/tomcat/webapps/ROOT/*.png /code/images
mv /soft/tomcat/webapps/ROOT/*.gif /code/images
Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.7 tomcat.oldxu.com
在浏览器中输入域名tomcat.oldxu.com即可访问。
2.动静分离实例
静态资源10.0.0.8
cat /etc/nginx/conf.d/ds.oldxu.conf
---------------/etc/nginx/conf.d/ds.oldxu.conf-------------------
server {
listen 80;
server_name ds.oldxu.com;
root /code/images;
location / {
index index.html;
}
}
-------------/etc/nginx/conf.d/ds.oldxu.conf结束------------------
mkdir /code/images;
cd /code/images
wget http://nginx.org/nginx.png
nginx -t
systemctl restart nginx
动态资源10.0.0.7:
vim /soft/tomcat/webapps/ROOT/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>Nginx+Tomcat动静分离</title>
</head>
<body>
<%
Random rand = new Random();
out.println("<h2>动态资源</h2>");
out.println(rand.nextInt(99)+100);
%>
<h2>静态图片</h2>
<img src="nginx.png" />
</body>
</html>
负载均衡10.0.0.5:
vim /etc/nginx/conf.d/proxy_ds.oldxu.conf
-----------/etc/nginx/conf.d/proxy_ds.oldxu.conf-------------
upstream java {
server 172.16.1.8:8080:
}
upstream static {
server 172.16.1.7:80;
}
server {
listen 80;
server_name ds.oldxu.com;
location ~* \.(png|gif|jpg)$ {
proxy_pass http://static;
include proxy_params;
}
location / {
proxy_pass http://java;
include proxy_params;
}
}
--------/etc/nginx/conf.d/proxy_ds.oldxu.conf结束-------------
nginx -t
systemctl restart nginx
Windows的c:\windows\system32\drives\etc\hosts中添加:
10.0.0.5 ds.oldxu.com
在浏览器中输入域名ds.oldxu.com即可访问。
3.python3+django开发的博客
#步骤一、安装 python3 的环境
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel \
sqlite-devel gcc gcc-c++ openssl-devel zlib zlib-devel python3 python3-devel -y
#步骤二、安装 Django 框架、uwsgi
[root@web01 ~]# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip
[root@web01 ~]# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ django==2.1.8
[root@web01 ~]# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ uwsgi
#步骤三、配置Django工程
[root@web02 ~]# cd /opt
[root@web02 opt]# django-admin.py startproject demosite
[root@web02 ~]# vim /opt/demosite/demosite/settings.py
#仅仅修改这一行
ALLOWED_HOSTS = ['*']
[root@web02 ~]# python3 /opt/demosite/manage.py runserver 0.0.0.0:9999
#步骤四、配置uWSGI、配置Nginx
[root@web02 ~]#cat /opt/demosite/uwsgi.ini
--------------/opt/demosite/uwsgi.ini-----------------
[uwsgi]
#uwsgi监听的端口
socket = 127.0.0.1:9999
#uwsgi启动进程数
workers = 2
#最大接收的请求数
max-requests = 1000
#buffer缓冲区大小
buffer-size = 30000
#进程pid存放路径
pidfile = /run/uwsgi.pid
#uwsgi日志存储路径
daemonize = /var/log/uwsgi.log
--------------/opt/demosite/uwsgi.ini结束-----------------
[root@web02 ~]# uwsgi --ini /opt/demosite/uwsgi.ini
[uWSGI] getting INI configuration from /opt/demosite/uwsgi.ini
[root@web02 ~]# netstat -lntp|grep 9999
tcp 0 0 127.0.0.1:9999 0.0.0.0:* LISTEN 32894/uwsgi
[root@web02 ~]# vim /etc/nginx/conf.d/djang.oldxu.com.conf
server {
listen 80;
server_name django.oldxu.com;
location / {
index index.html;
uwsgi_pass 127.0.0.1:9999;
uwsgi_param UWSGI_CHDIR /opt/demosite; #工程所在的目录
uwsgi_param UWSGI_SCRIPT demosite.wsgi; #demosite/wsgi接口文件 /opt/demosite/demosite/wsgi.py
include uwsgi_params;
}
}