您的位置:首页 > 运维架构 > Nginx

Nginx 学习笔记

2015-12-07 03:02 741 查看
nginx 以小巧,并发能力强而著称,其运行速度非常快。

下面以示例介绍我的学习过程。

第一次尝试使用Nginx,对其原理和使用方式都是摸索式的进行,所以所有测试都在本机完成。

一、源码编译安装

[code]wget http://nginx.org/download/nginx-1.7.1.tar.gz[/code] 
为了不至于与
sudo apt-get nginx
的安装目录搞混淆,在这里我指定安装目录为
/home/david/opt/nginx


[code]./configure --prefix=/home/david/opt/nginx  
make && make install


二、测试

为方便调试,切换目录到
/home/david/opt/nginx


[code] 启动 nginx:  ./nginx

 停止 nginx:  ./nginx -s stop 

 关闭 nginx:  ./nginx -s quit 

 重新加载配置:./nginx -s reload 

 http://localhost   显示nginx的欢迎页


三、 常用配置

3.1 当没有索引页时,显示文件和目录列表

编辑配置文件
/home/david/opt/nginx/conf/nginx.conf


[code]    worker_processes 1;   #服务进程数量,一般等于CPU数量   

    events {  
        useepoll;                   #linux上使用epoll网络模型,可不配  
        worker_connections 1024;    #一个worker_processe允许的最近并发连接数量  
    }  

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

        autoindex on;               # 显示文件列表  
        autoindex_exact_size on;  
        autoindex_localtime on;   
    }


3.2 虚拟文件目录

nginx 好像没有虚拟目录这个概念,但是可以通过指定请求路径时的访问路径实现,具体通过 alias 和 root 这两个参数设置,这两个参数非常容易混淆,导致出现404错误

现在要访问:
/home/david/web/
form_get.html
这个文件,如下配置:

[code]    # alias 实现  
    location ^~/web/ {  
        alias /home/scada/david/web/;
        index index.html;  

    autoindex on;               # 显示文件列表  
        autoindex_exact_size on;  
        autoindex_localtime on;   
    }  

    # root 实现  
    location ^~/web/ {  
        root /home/david;
        index index.html;

    autoindex on;               # 显示文件列表  
        autoindex_exact_size on;  
        autoindex_localtime on;      
    }


浏览器访问:http://127.0.0.1/web/

由于该目录没有索引文件,所有浏览器会显示目录树:



通过访问:http://127.0.0.1/web/form_get.html

效果如下:



参考

http://tcspecial.iteye.com/blog/2077510

CGI热门博客
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: