设为首页 收藏本站
查看: 1952|回复: 0

[经验分享] 搭建nginx服务器及文件的配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-9-25 13:41:52 | 显示全部楼层 |阅读模式
一、搭建nginx服务器及平滑升级

1.搭建基本的nginx服务器
准备nginx-0.8和nginx-1.0两个源码包
[iyunv@localhost nginx-package]# tar -zxf nginx-0.8.55.tar.gz
[iyunv@localhost nginx-package]# tar -zxf nginx-1.0.5.tar.gz
关闭HTTP服务,否则端口被占用
[iyunv@localhost ~]# service httpd stop
创建nginx运行时的所有者
[iyunv@localhost ~]# useradd -s /sbin/nologin -M www
[iyunv@localhost ~]# tail -1 /etc/passwd
www:x:500:500::/home/www:/sbin/nologin
[iyunv@localhost ~]# tail -1 /etc/group
www:x:500:
安装开发工具
[iyunv@localhost nginx-package]# yum -y install gcc*
[iyunv@localhost ~]# yum -y install pcre-devel
[iyunv@localhost nginx-package]# cd nginx-0.8.55
[iyunv@localhost nginx-0.8.55]# ./configure --help
[iyunv@localhost nginx-0.8.55]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
如果最后提示没有安装openssl库需要重新安装
[iyunv@localhost nginx-0.8.55]# yum -y install openssl-devel
重新执行配置
[iyunv@localhost nginx-0.8.55]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
[iyunv@localhost nginx-0.8.55]# make && make install
启动服务
[iyunv@localhost nginx-0.8.55]# /usr/local/nginx/sbin/nginx
[iyunv@localhost nginx-0.8.55]# netstat -anput | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      8466/nginx      
[iyunv@localhost nginx-0.8.55]# elinks --dump 192.168.118.5
                               Welcome to nginx!

2.平滑升级
[iyunv@localhost nginx-package]# cd nginx-1.0.5
查看当前版本和配置信息
[iyunv@localhost nginx-1.0.5]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/0.8.55
TLS SNI support disabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
[iyunv@localhost nginx-1.0.5]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
[iyunv@localhost nginx-1.0.5]# make
将之前的版本移除,将新版本的执行文件移动过去
[iyunv@localhost nginx-1.0.5]# mv /usr/local/nginx/sbin/{nginx,nginx-low}
[iyunv@localhost nginx-1.0.5]# cp objs/nginx /usr/local/nginx/sbin/
平滑升级
[iyunv@localhost nginx-1.0.5]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
查看现在版本
[iyunv@localhost nginx-1.0.5]# /usr/local/nginx/sbin/nginx -v
nginx: nginx version: nginx/1.0.5
发现已经成功升级
[iyunv@localhost nginx-1.0.5]# elinks --dump 192.168.118.5
                               Welcome to nginx!
关闭服务
[iyunv@localhost nginx-1.0.5]# /usr/local/nginx/sbin/nginx -s stop


二、nginx虚拟主机
客户端:192.168.1.1
nginx服务器:192.168.1.2
1.基于域名的虚拟主机
配置nginx服务器
[iyunv@localhost conf]# cd /usr/local/nginx/conf/
分离出有用配置
[iyunv@localhost conf]# grep -vE "#|^$" nginx.conf.default > nginx.conf
[iyunv@localhost conf]# vim nginx.conf
添加虚拟主机
server {
       listen       80;
       server_name  www.baidu.com;
       location / {
           root /baidu;
           index index.html index.htm;
    }
    }
    server {
       listen       80;
       server_name  www.qq.com;
       location / {
           root /qq;
           index index.html index.htm;
    }
    }
[iyunv@localhost conf]# /usr/local/nginx/sbin/nginx -s stop
[iyunv@localhost conf]# /usr/local/nginx/sbin/nginx
[iyunv@localhost conf]# echo "baidu" > /baidu/index.html
[iyunv@localhost conf]# echo "qq" > /qq/index.html

配置客户机
添加域名解析
[iyunv@www ~]# vim /etc/hosts
添加
192.168.1.2 www.baidu.com www
192.168.1.2 www.qq.com www
[iyunv@www ~]# elinks --dump www.baidu.com
   baidu
[iyunv@www ~]# elinks --dump www.qq.com
   qq
实现了基于域名的虚拟主机的访问

2.基于端口的虚拟主机
修改nginx服务器的配置文件
[iyunv@localhost conf]# vim nginx.conf
修改
server {
       listen      192.168.1.2:80;
       server_name  www.baidu.com;
       location / {
           root /baidu;
           index index.html index.htm;
    }
    }
    server {
       listen       192.168.1.2:8080;
       server_name  www.qq.com;
       location / {
           root /qq;
           index index.html index.htm;
    }
    }
[iyunv@localhost conf]# /usr/local/nginx/sbin/nginx -s stop
[iyunv@localhost conf]# /usr/local/nginx/sbin/nginx

客户机测试
[iyunv@www ~]# elinks --dump 192.168.1.2:80
   baidu
[iyunv@www ~]# elinks --dump 192.168.1.2:8080
   qq
实现了基于端口的虚拟主机的访问

3.基于IP地址的虚拟主机
添加子接口
[iyunv@localhost ~]# ifconfig eth0:1 192.168.1.3
[iyunv@localhost ~]# ifconfig eth0:1
eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:FD:5B:B1  
          inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0
修改nginx服务器的配置文件
[iyunv@localhost conf]# vim nginx.conf
server {
       listen      192.168.1.2:80;
       server_name  www.baidu.com;
       location / {
           root /baidu;
           index index.html index.htm;
    }
    }
    server {
       listen       192.168.1.3:80;
       server_name  www.qq.com;
       location / {
           root /qq;
           index index.html index.htm;
    }
    }

客户端测试
[iyunv@www ~]# elinks --dump 192.168.1.2
   baidu
[iyunv@www ~]# elinks --dump 192.168.1.3
   qq
实现了基于IP地址的虚拟主机的访问




三、防盗链
目标:防止其他网站通过超链接盗用本地网站的图片、视频等资源
nginx服务器:192.168.2.1
http服务器:192.168.2.2
1.修改nginx服务器
在nginx服务器的主目录里面添加一个图片
[iyunv@localhost conf]# cp /root/Desktop/one.png /usr/local/nginx/html/
[iyunv@localhost conf]# /usr/local/nginx/sbin/nginx -s stop
[iyunv@localhost conf]# /usr/local/nginx/sbin/nginx
[iyunv@localhost conf]# elinks --dump 192.168.2.2
                               Welcome to nginx!
2.配置http服务器
[iyunv@localhost ~]# yum -y install httpd
[iyunv@localhost ~]# service httpd start
建立盗链网页
[iyunv@localhost ~]# vim /var/www/html/115.html
<html>
<body><a href=http://192.168.2.2/one.png>this Images</a>
</body>
</html>
[iyunv@localhost ~]# elinks --dump 192.168.2.1
                               Welcome to nginx!
[iyunv@localhost ~]# elinks --dump 192.168.2.2
浏览器测试盗链网页
http://192.168.2.2/115.html
点击超链接能够显示图片,盗用了nginx服务器网站的图片

3.配置nginx服务器
修改nginx的配置文件
[iyunv@localhost conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
#添加防盗链设置
        location ~* \.(png|jpg|jif|flv)$ {
           valid_referers none blocked www.tarena.com tarena.com;
            if ($invalid_referer) {
               return 404;
             }
        }
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[iyunv@localhost conf]# /usr/local/nginx/sbin/nginx -s stop
[iyunv@localhost conf]# /usr/local/nginx/sbin/nginx
4.http服务器验证
浏览器验证
http://192.168.2.2/115.html
点击超链接,出现404错误,实现了防盗链


四、访问控制和用户认证  (控制客户端对网站服务器的访问)
目标:实现nginx服务器对客户机根据IP地址,网段进行访问控制
      允许的IP地址还可以输入用户名和密码进行认证登陆增加安全性
客户机IP:192.168.1.1
nginx服务器IP:192.168.1.2
1.访问控制
修改nginx配置文件
[iyunv@localhost conf]# cd /usr/local/nginx/conf/
[iyunv@localhost conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
#拒绝192.168.1.1访问,允许其他所有主机访问
            deny 192.168.1.1;
            allow all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[iyunv@localhost conf]# ../sbin/nginx -s stop
[iyunv@localhost conf]# ../sbin/nginx
客户机测试
[iyunv@www ~]# ifconfig eth0 | head -2
eth0      Link encap:Ethernet  HWaddr 00:0C:29:C7:AD:28  
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
[iyunv@www ~]# elinks --dump 192.168.1.2
                                 403 Forbidden
更换IP地址重新访问
[iyunv@www ~]# ifconfig eth0 192.168.1.11
[iyunv@www ~]# elinks --dump 192.168.1.2
                               Welcome to nginx!
2.用户认证
修改nginx配置文件
[iyunv@localhost conf]# vim nginx.conf
修改
location / {
            root   html;
            index  index.html index.htm;
            deny 192.168.1.1;
            allow all;
            auth_basic "please input your username and password";
            auth_basic_user_file /usr/local/nginx/user.txt;
        }
[iyunv@localhost conf]# ../sbin/nginx -s stop
[iyunv@localhost conf]# ../sbin/nginx

生成user.txt认证文件
[iyunv@localhost conf]# yum -y install httpd
添加两个认证的用户admin,feng 如果文件已经存在不需要-c选项
[iyunv@localhost conf]# htpasswd -c /usr/local/nginx/user.txt admin
[iyunv@localhost conf]# htpasswd /usr/local/nginx/user.txt feng
[iyunv@localhost conf]# cat /usr/local/nginx/user.txt
admin:sfbEMSjDZbA4.
feng:4SH4NvORhXMFs

客户机用浏览器测试
http://192.168.1.2
输入刚刚添加用户名和密码
访问成功!



五、nginx反向代理(分发服务器 后端服务器状态设置)
目标:外网客户机能够通过nginx服务器访问内网的web服务器,并可以配置nginx来实现不同的分发策略
准备环境:
外网客户端:192.168.1.1
nginx代理服务器:192.168.1.2(外网IP地址) 192.168.2.1(内网IP地址)
内网web1:192.168.2.2
内网web2:192.168.2.3
保证外网客户机,两台内网web服务器分别与nginx互通
1.在两台web服务器上搭建HTTP服务(web1 web2)
web1:
[iyunv@localhost ~]# yum -y install httpd
[iyunv@localhost ~]# service httpd start
[iyunv@localhost ~]# echo "web1" > /var/www/html/index.html
测试本机能否访问
[iyunv@localhost ~]# elinks --dump 192.168.2.2
   web1
web2:
[iyunv@localhost ~]# yum -y install httpd
[iyunv@localhost ~]# service httpd start
[iyunv@localhost ~]# echo "web2" > /var/www/html/index.html
测试本机能否访问
[iyunv@localhost ~]# elinks --dump 192.168.2.3
   web2
2.配置nginx服务器
[iyunv@localhost conf]# cd /usr/local/nginx/conf/
分离出有用配置
[iyunv@localhost conf]# grep -vE "#|^$" nginx.conf.default > nginx.conf
[iyunv@localhost conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
#添加分发组
    upstream "webgroup" {
        server 192.168.2.2:80;
        server 192.168.2.3:80;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
          #  root   html;
          #  index  index.html index.htm;
#将80端口转发到分发组里的web服务器上
             proxy_pass  http://webgroup;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
重启服务
[iyunv@localhost conf]# ../sbin/nginx -s stop
[iyunv@localhost conf]# ../sbin/nginx
3.外网客户机测试
[iyunv@www ~]# elinks --dump 192.168.1.2
   web2
[iyunv@www ~]# elinks --dump 192.168.1.2
   web1
4.更改nginx的分发策略
(1)默认情况下是轮询的方式
(2)weight  指定轮询几率
              权重和访问比率成正比
              通常用于后断服务器性能不同的情况
               默认值为1
修改nginx的配置文件
[iyunv@localhost conf]# vim nginx.conf
修改
    upstream "webgroup" {
        server 192.168.2.2:80 weight=1;
        server 192.168.2.3:80 weight=3;
    }
[iyunv@localhost conf]# ../sbin/nginx -s stop
[iyunv@localhost conf]# ../sbin/nginx
客户机测试
[iyunv@www ~]# elinks --dump 192.168.1.2
   web1
[iyunv@www ~]# elinks --dump 192.168.1.2
   web2
[iyunv@www ~]# elinks --dump 192.168.1.2
   web2
[iyunv@www ~]# elinks --dump 192.168.1.2
   web2
(3)其他分发策略
ip_hash  每个请求按访问ip的hash结果分配
              这样可以让每个访客固定访问一个后端服务器 ,可以解决session的问题
down:    表示当前server暂时不参与负载
max_fails:允许请求失败的次数(默认为1), 当超过此次数时,返回
backup:当其他所有的非backup机器down或者忙的时候,请求会发给backup机器,所以这台机器压力会最轻
upstream   sergrp  {
        #ip_hash;
        #server 192.168.8.5:80    weight=2;
        server 192.168.8.5:80   down;
        server 192.168.8.4:80;
        server 192.168.8.6:80   backup;
        server 192.168.8.3:80   max_fails=2   fail_timeout=30s;
}


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-25411-1-1.html 上篇帖子: nginx限速 下篇帖子: TFS的nginx模块配置 服务器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表