nginx安装部署
解压pcre
tar zxf pcre-8.30.tar.gz
cd pcre-8.30/
./configure
make && make install
安装pcre库是为了兼容nginx rewrite
解压openssl
tar zxf openssl-1.0.0a.tar.gz
解压nginx
useradd nginx -M -s /sbin/nologin
添加用户(不允许登录)
tar -zxf nginx-1.1.1.tar.gz
cd nginx-1.1.1
./configure --user=nginx --prefix=/myappsoft/nginx-1.1.1 --with-openssl=../openssl-1.0.0a/ --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
make && make install
检查nginx语法
[root@localLinuxMySql nginx-1.1.1]# /myappsoft/nginx-1.1.1/sbin/nginx -t
nginx: the configuration file /myappsoft/nginx-1.1.1/conf/nginx.conf syntax is ok
nginx: configuration file /myappsoft/nginx-1.1.1/conf/nginx.conf test is successful
设置全局变量及开机启动
#增加软链接,方便升级
[root@localLinuxMySql nginx-1.1.1]# ln -s /myappsoft/nginx-1.1.1 /myappsoft/nginx
#查看软链接是否成功,如果闪动就没成功
[root@localLinuxMySql nginx-1.1.1]# ll -d /myappsoft/nginx
lrwxrwxrwx 1 root root 23 Feb 10 19:05 /myappsoft/nginx -> /myappsoft/nginx-1.1.1
#将Nginx命令加入系统全局变量,启动nginx
[root@localLinuxMySql nginx-1.1.1]# echo 'export PATH=$PATH:/myappsoft/nginx/sbin' >>/etc/profile
#使全局变量修改生效
[root@localLinuxMySql nginx-1.1.1]# source /etc/profile
#加入开机自启动
[root@localLinuxMySql nginx-1.1.1]# echo '/myappsoft/nginx/sbin/nginx' >> /etc/rc.local
配置内网服务器指向及负载均衡
#找到/myappsoft/nginx-1.1.1/conf目录下nginx.conf文件,修改以下内容
upstream webserver1 {
server 192.168.11.12:8091 weight=3;
}
upstream webserver2 {
server 192.168.0.12:8092 weight=3
}
upstream webserver3 {
server 192.168.0.12:8092 weight=3
server 192.168.0.13:8092 weight=3
}
server {
listen 8080;
server_name 192.168.1.195;
location /testapp {
proxy_pass http://webserver1;
}
location /testapp2 {
proxy_pass http://webserver2;
}
location /testapp3 {
proxy_pass http://webserver3;
}
}
参数解释:
upstream webserver { } 定义真实服务器组
server {} 定义一个服务配置
listen 8080 监听8080端口
server_name 监听的地址或IP
location / 匹配server_name后的url或IP
proxy_pass 代理参数,后接upstream定义的服务器组
启动/重启nginx
#启动nginx
[root@localLinuxMySql nginx-1.1.1]# nginx
#重启nginx
[root@localLinuxMySql nginx-1.1.1]# nginx -s reload
查看监听端口
[root@localLinuxMySql nginx-1.1.1]# netstat -plnt |grep 8080
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 32716/nginx: master
测试访问
访问:http://192.168.1.195:8080/testapp 实际是映射地址:http://192.168.11.12:8091/testapp
访问:http://192.168.1.195:8080/testapp2 实际是映射地址:http://192.168.0.12:8092/testapp2
testapp3 可以映射两台服务器地址 实现负载均衡
- 本文标签: 其他
- 本文链接: http://www.it586.cn/article/11
- 版权声明: 本文由miger原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权