几个简单使用nginx的例子—一代
前言:本人主攻后端,简单了解运维,如有错误,请您指正
云服务器安装nginx
安装nginx所需依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
解压nginx 在/usr/src目录会解压到/usr/local中
tar –xvf pcre-8.37.tar.gz
编译
./configure
make && make install...
常用命令
存放在/usr/local/nginx/sbin中
./nginx -v
./nginx
./nginx -s stop
./nginx -s reload
反向代理
nginx.conf 在/usr/local/nginx/conf目录中
效果:访问 47.101.33.159:80 转发到47.101.33.159:8080
修改server即可
server {
listen 80;
server_name 47.101.33.159;
location / {
root html;
proxy_pass http://47.101.33.159:8080;
index index.html index.htm;
}
效果:访问47.101.33.159:9000/mydir/mine.html 转发到47.101.33.159:8081/mydir/mine.html
访问了在tomcat下webappmydir中的文件
添加一个server
server {
listen 9000;
server_name 47.101.33.159;
location ~ /mydir/ {
proxy_pass http://47.101.33.159:8081;
}
负载均衡
效果:访问47.101.33.159:80/mydrir/mine.html,第一次访问8080,第二次访问8081,轮转
upstream myserver {
server 101.43.85.67:8080;
server 101.43.85.67:8081;
}
server {
listen 80;
server_name 101.43.85.67;
location / {
root html;
proxy_pass http://myserver;
index index.html index.htm;
}
动静分离
效果:静态资源不通过tomcat进行访问,直接映射
访问http://101.43.85.67/image/wechat.jpg 该图片地址为/mydata/image/wechat.jpg
server {
listen 80;
server_name 101.43.85.67;
location /www/ {
root /mydata/;
index index.html index.htm;
}
location /image/ {
root /mydata/;
autoindex on;
}
|