linux中nginx的安装和配置虚拟主机以及模拟配置nginx反向代理

工具:nginx-1.8.0.tar.gz
下载:https://download.csdn.net/download/ly_linyuan/10385616

安装nginx
1.在线安装 c语言编写的,需安装gcc的环境:yum install gcc-c++

2.第三方开发包:yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel

3.使用sftp模式上传nginx-1.8.0.tar.gz

4.解压缩:tar zxf nginx-1.8.0.tar.gz
这里写图片描述

5.进入nginx目录,使用configure命令创建一makeFile文件

cd nginx-1.8.0

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

这里写图片描述
这里写图片描述

6.编译源代码:make
这里写图片描述
7.安装:make install
这里写图片描述

8.查看:cd /usr/local/
这里写图片描述

9.进入启动目录:nginx:cd sbin/ 启动: ./nginx
这里写图片描述

注意:如果报错,执行接下来的操作
.创建多级目录再次启动: mkdir /var/temp/nginx -p

10.相关操作:
查看进程:ps aux|grep nginx
关闭进程: ./nginx -s stop
刷新配置:./nginx -s reload
这里写图片描述

11.通过浏览器访问ip我这里是(192.168.64.129)
如果访问不了,可能是没有关闭防火墙
(关闭防火墙)service iptables stop
(永久关闭)chkconfig iptables off
这里写图片描述

配置虚拟主机
1.进入到conf目录下编辑nginx.conf文件: vim nginx.conf
这里写图片描述
2.在vim模式下在需要复制的server节点地方开头按shift和v键,松开后使用上下键选择
需要复制的内容,按键y进行复制,然后选择粘贴的地方按键p进行粘贴,按键u为撤销

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   D:/javafreemaker/;
        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;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

2.1也可以使用Editplus进行操作
2.1.1打开Editplus,选择文件下的Ftp下的Ftp设置
这里写图片描述
2.1.2添加
描述:自己随意命名,这里为(192.128.64.129)
FTP服务器:为安装nginx的主机的ip,这里为(192.128.64.129)
用户名:为linux主机的用户名
密码:为linux主机的密码
这里写图片描述
2.1.3点击高级选项
加密方式:选择sftp
端口:为22
这里写图片描述
2.1.4确定后即可在Editplus左边倒三角图标中找到名为192.128.64.129的ftp连接,然后进行操作
这里写图片描述
这里写图片描述
这里写图片描述

3.将复制的server节点的端口listen改为81,将默认的页面改为html81(随意命名)
不需要代码的可以删除

server {
    listen       81;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html81;
        index  index.html index.htm;
    }
}

注意在Editplus·中添加后去linux中查看是否添加成功

4.在nginx目录下将html复制一份:cp -r html/ html81
这里写图片描述

5.将默认的首页html81改动一下: vim html81/index.html
将welcome to nginx!改为welcome to nginx-81!便于区分(按i键为在光标出插入 wq保存退出)
这里写图片描述
6.通过浏览器访问,这里端口为81(192.168.64.129:81)
这里写图片描述

模拟配置nginx反向代理
1.将tomcat文件通过sftp上传到linux中,解压:tar zxf tomcat文件名

2.复制两份tomcat: cp tomcat文件名 tomcat01 -r

3.想要两个tomcat同时启动,需要改端口号

4.将tomcat02的端口号改为81
vim tomcat02/conf/server.xml
8005-8006
8080-8081
8009-8010

5.启动tomcat01和tomcat02
tomcat01/bin/startup.sh

6.通过浏览器访问ip(192.168.64.129:80)(192.168.64.129:81)
这里写图片描述
这里写图片描述
7.将tomcat首页index.jsp中的title改动一下便于区分

vim tomcat01/webapps/ROOT/index.jsp
${pageContext.servletContext.serverInfo}-8080
${pageContext.servletContext.serverInfo}-8081

退出并保存
这里写图片描述
这里写图片描述
8.需要配置nginx反向代理
在nginx的nginx.conf中添加(两个)新的server结点

upstream tomcat1{
    server 192.168.64.129:8080;
}

server {
    listen       80;
    server_name  www.mytest1.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        proxy_pass   http://tomcat1;
        index  index.html index.htm;
    }

}

upstream tomcat2{
    server 192.168.64.129:8081;
}

server {
    listen       80;
    server_name  www.mytest2.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        proxy_pass   http://tomcat2;
        index  index.html index.htm;
    }

}

9.通过浏览器访问路径www.mytest1.com和www.mytest2.com就可以访问到tomcat8080和tomcat8081
这里写图片描述
这里写图片描述

10.如果www.mytest1.com访问人数过多,则加一个tomcat03,端口号为8082
upstream tomcat1{
server 192.168.64.129:8080;
server 192.168.64.129:8082;
}
这里写图片描述
这里写图片描述

11.nginx会自动实现负载均衡,轮流访问
第一次访问:
这里写图片描述
刷新后第二次访问:
这里写图片描述
12.如果后加的tomcat03为16核,原来的tomcat01为6核,访问不可轮流,则需要调试权重
upstream tomcat1{
server 192.168.64.129:8080 weight=1;
server 192.168.64.129:8082 weight=2;
}
则访问到8082端口的概率会高一些

可能遇到的问题:(访问不了)需要改一下本地的host,这样浏览器输入这个域名就能解析到虚拟机的IP了
在C:\Windows\System32\drivers\etc目录下,修改host文件
需要注意的是,host在当前目录下面用户是没有修改权限的,所以你需要先复制出来到桌面,然后用记事本打开修改,复制回去替换。
在文件末尾加上
192.168.64.129 www.mytest1.com
192.168.64.129 www.mytest2.com
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值