码迷,mamicode.com
首页 > Web开发 > 详细

【初学菜鸟作-nginx网站服务器的安装以及基本使用】

时间:2014-07-18 13:06:17      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:nginx

nginx网站服务的安装与配置

1.编写脚本,以安装nginx

编写脚本前须知nginx的安装环境

此案例提供两个版本的nginxnginx-0.8.55.tar.gz  nginx-1.0.5.tar.gz

其为源码包,需要gcc环境(gcc gcc-c++ make)以及pcrepcre-devel  ssl openssl

root家目录下新建脚本文件install.sh

[root@localhost ~]# cat install.sh

 

#!/bin/bash

 

service httpd stop > /dev/null

                            --关闭http服务对端口80的占用

chkconfig httpd off > /dev/null

 

cd nginx-package

                                          --进入安装包所在目录

tar -zxf nginx-0.8.55.tar.gz

    

tar -zxf nginx-1.0.5.tar.gz

 

cd nginx-0.8.55

 

useradd -M -s /sbin/nologin www

                       --新建一个为服务进行调度的用户

yum -y install prce prce-devel >/dev/null

                   --安装依赖包

yum -y install openssl* > /dev/null

 

./configure --prefix=/usr/local/nginx--user=www --group=www --with-http_stub_status_module  --with-http_ssl_module

 

         --prefix为指定安装位置with-http_stub_status_module

make

                                                        --编译安装

make install

 

2.配置基于域名的虚拟主机

 

       www .tarena.com

 

       bbs .tarena.com

 

只允许从ip 192.168.13.1主机访问  bbs  .tarena.com  8080 端口  访问时要提交正确的用户adm  密码888  方可访问

[root@localhost ~]# cd/usr/local/nginx/sbin/                --进入执行程序目录

 

[root@localhost sbin]# ./nginx                               --开启服务

[root@localhost sbin]# cd/usr/local/nginx/conf/

              --进入配置文件目录

[root@localhost conf]# cp nginx.confnginx.confbak     --备份

[root@localhost conf]# mkdir ../html/www

 

[root@localhost conf]# mkdir ../html/bbs

 

[root@localhost conf]# echo www >../html/www/index.html

 

[root@localhost conf]# echo bbs >../html/bbs/index.html

 

[root@localhost conf]# yum -y installhttpd                   --安装http服务是为例使用它的                                                                htpasswd设定密码访问控制功能

[root@localhost conf]# htpasswd -c/usr/local/nginx/conf/authuser.txt adm

                                                                 --新建用户adm

 

[root@localhost conf]# cat 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;

 

       }

 

       error_page  500 502 503 504  /50x.html;

 

       location = /50x.html {

 

            root   html;

 

       }

 

   }

 

server {

 

     listen80;                                       --监听端口为80

 

     server_namewww.tarena.com;

                   --服务域名

     location/ {

 

         root/usr/local/nginx/html/www;

              --访问主页位置

         indexindex.html;

                            --主页名

              }

 

     }

 

server {

 

       listen 192.168.13.5:8010;

                   --通过IP地址访问,端口为8010

      server_name www.tarena.com;

 

       location / {

 

                root/usr/local/nginx/html/www;

 

                indexindex.html;

 

                   }

 

       }

 

server {

 

     listen8080;

                                          --监听端口为80

     server_namebbs.tarena.com;

                   --服务域名

     location/ {

    

         root/usr/local/nginx/html/bbs;

              --访问主页位置

         indexindex.html;

                            --主页名

           allow 192.168.13.1;

                            --允许访问的主机

         denyall

                                   --拒绝所有

         auth_basic" 请输入用户名密码" --访问时的提示信息

 

         auth_basic_user_file/usr/local/nginx/conf/authuser.txt

     --帐号密码读取位置

              }

 

     }

 

}

 

通过客户机测试效果

[root@localhost 桌面]# elinks --dump http://www.tarena.com

 

  www

 

通过网页访问bbs.tarena.com时会要求输入用户名与密码,输入后将看到其网页文件内容

3.通过脚本进行nginx的在不关闭的请光下平滑升级

[root@localhost ~]# cat update.sh

 

#!/bin/bash

 

cd /root/nginx-package/nginx-1.0.5                   --进入高版本安装目录

 

./configure --prefix=/usr/local/nginx--user=www --group=www --with-http_stub_status_module –with-http_ssl_module

         --执行操作要与低版本一致

make

                                                   --编译

mv /usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginxold

     --将老版本执行程序改名

mv objs/nginx /usr/local/nginx/sbin/

                                 --将新版本执行程序移动到安装目录

make upgrade                                                     --进行平滑升级

 

4.nginx反向代理服务器的配置以及不同需求访问的分离

案例如下:使用Ip地址1.1.1.1的服务器做nginx反向代理服务器,当接收到用户发给自己的连接请求http://www.baidu.com   代替用户访问内网的网站服务器。(当用户访问以.php结尾的文件时,代替用户连接内网的网站服务器1.1.1.21.1.1.3 ,ip2机器配置比较高 让他的响应次数比ip 3的主机用户访问以.html结尾的文件时到本机nginx安装目录下html子目录下获取网页文件,并对此网页目录做防盗链设置。)

              http{

              upstreamwebgrp {

                                 --定义组

       server 1.1.1.2:80 weight=3;

                       --weight定义访问权重,默认1

       server 1.1.1.3:80;

       }

server {

       listen 80;

       server_name www.baidu.com;

                     --服务器名

       location ~ \.php$ {

                                 --php结尾去找下面的组

       proxy_pass http://webgrp;

       }

       location ~ \.html$ {

                                 --html结尾去找下面的主机

       proxy_pass http://1.1.1.1;

       }

       location ~* \.(gif|ipg|png|swf|flv|htm)$ {         --外链不允许的网页格式  

       valid_referers none blocked www.baidu.com .baidu.com;

     --允许外链的主机和域

       if ($invalid_referer) {

                                

       rewrite ^/ http://1.1.1.1/error.html;

              --如果不允许跳转到此页

       #return 404;

       }

       }

         web1  1.1.1.2                                 外链(贼)服务器192.168.13.3

                   1.1.1.1 nginx代理 192.168.13.1      

         web2   1.1.1.3                                测试客户机192.168.13.2

 

         测试时在外链服务器新建外链页指向nginx代理,如下

         [root@localhosthtml]# cat index.html

<html>

<body>

       <ahref="http://www.baidu.com/index.htm">fangdanlianceshi</a>

</body>

</html>

         通过客户端访问外链服务器查看时候外链是否被禁止

 


【初学菜鸟作-nginx网站服务器的安装以及基本使用】,布布扣,bubuko.com

【初学菜鸟作-nginx网站服务器的安装以及基本使用】

标签:nginx

原文地址:http://yeyue.blog.51cto.com/9037000/1439712

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!