nginx学习文档持续不定期更新

来源:互联网 发布:windows7装ubuntu系统 编辑:程序博客网 时间:2024/06/05 05:17

安装nginx

方法一:直接安装

wget http://nginx.org/download/nginx-1.5.11.tar.gztar -zxvf nginx-1.5.11.tar.gzsudo apt-get install gccsudo apt-get install g++sudo apt-get install makesudo apt-get install libz-devsudo apt-get install libbz2-devsudo apt-get install libreadline-devwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gztar -zxvf pcre-8.34.tar.gzcd pcre-8.34/ ./configuresudo makesudo make installcd nginx-1.5.11./configuresudo makesudo make install


方法二:安装openresty

首先安装pcrewget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gzcd pcre-8.35./configuremakemake install然后安装openrestywget http://openresty.org/download/ngx_openresty-1.5.8.1.tar.gztar xzvf ngx_openresty-VERSION.tar.gzcd ngx_openresty-VERSION/./configure --with-luajit --with-pcre=../pcre-8.35makemake install

程序员都要写的hello world

建立一个/home/cpadmin/test_nginx目录,来存放测试用例。

在/usr/local/nginx/conf/nginx.conf中http模块中添加一个server模块:

server{    listen 8011;    server_name localhost;    charset utf-8;    location / {        alias /home/cpadmin/test_nginx/;    }}

启动nginx:进入/usr/local/nginx,输入sudo ./sbin/nginx

在/home/cpadmin/test_nginx目录下写一个index.html:

<html><head></head><body>hello world</body></html>

访问localhost:8011/index.html出现hello world

常用命令(在/usr/local/nginx/目录下)

启动nginx: sudo ./sbin/nginx

停止nginx:

sudo ./sbin/nginx -s stop

sudo ./sbin/nginx –s quit

nginx重载配置:sudo ./sbin/nginx –s reload

检查配置文件是否正确: sudo ./sbin/nginx –t

显示nginx帮助信息: sudo ./sbin/nginx –h

添加模块(以memcached为例)

将模块解压

进入nginx文件夹:./configure --prefix=/usr/local/nginx/ --add-module=/home/cpadmin/memc-nginx-module-0.14

make

make install

在/usr/local/nginx/下 输入 ./sbin/nginx –V如果有模块的话就证明添加上了。

nginx proxy_pass

让nginx服务器代理百度首页:

配置nginx.conf文件

server {       listen 8001;       location /{           proxy_pass http://www.baidu.com:80;       }}

之后访问localhost:8001就直接跳转到了百度页面

nginx memc srcache实现页面缓存

nginx配置:

server {       listen   7000;       server_name  www.gfan.com;       location = /memc{           internal;           memc_connect_timeout 100ms;           memc_send_timeout 100ms;           memc_read_timeout 100ms;           memc_ignore_client_abort on;           set $memc_key $query_string;           set $memc_exptime 300;           memc_pass 127.0.0.1:11211;       }       location / {           set $key $uri;           srcache_fetch GET /memc $key;           srcache_store PUT /memc $key;           srcache_store_statuses 200 301 302;           proxy_pass http://www.gfan.com;       }       error_log  logs/nginx_gfan_error.log debug;       access_log logs/nginx_gfan_access.log;}

缓存的页面是www.gfan.com,每次请求先去memc查询是否命中,如果命中则直接从memc中返回页面,如果没命中则去访问www.gfan.com然后将所得页面缓存到memc中。

写一个模块输出hello world(待完成)

遇到的问题:

nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

解决方法:

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

./sbin/nginx: /usr/local/lib/libcrypto.so.1.0.0: no version information available (required by ./sbin/nginx)

nginx: [alert] kill(13615, 3) failed (3: No such process)

解决方法:

ps aux | grep nginx 将进城全部杀掉,然后重新开启。


0 0