`

nginx的简介,安装和简单配置

阅读更多
nginx的简介,安装和简单配置


转载请注明原文链接:http://blog.csdn.net/omohe/archive/2009/07/09/4335763.aspx

0. nginx在实际中使用:
之前我们介绍了使用nginx可以完全作为web app server来使用,代替Apache;
另外也可以单单使用nginx作为反向代理服务器来实现Server Cluster。


1. nginx的基础知识:
参考http://en.wikipedia.org/wiki/Nginx
http://wiki.nginx.org/Main
http://www.nginx.org/
简单来说,精华在于:HTTPServer或者reverse proxy
另 外需要注意它和ApacheServer的不同,Unlike traditional servers, Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but most importantly, predictable amounts of memory under load.
Nginx scales in all directions: from the smallest VPS all the way up to clusters of servers.
另外,更多的功能可以通过模块进行扩展。

2. nginx的安装:
具体可以参考:http://wiki.nginx.org/Main( 支持Windows和Linux等多种OS)
1) 源代码安装很简单:可以按照默认的./configure,然后make和make install
输入./configure --help可以配置需要编译的各种模块,

如果日后需要加入某个模块的话,需要重新编译,具体:
make clean,然后./configure+新的配置选项, make, make install。

难点是如何理解nginx服务器的配置,位于安装目录的nginx.conf文件。
总之需要知道的是:具体如何配置决定于准备使用nginx实现什么功能,例如最简单的单单是个反向代理服务器,或者作为一个http server来使用。各种具体应用和配置可以参看一下:
    "Typical Configurations Overview For Nginx HTTP(S) Reverse Proxy/Web Server":
    http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/
更多详情参看关于如何配置的教程:
    http://wiki.nginx.org/NginxConfiguration

默认安装到/usr/local/nginx,具体启动的时候需要到bin目录下执行sudo ./nginx;
要终止nginx可以查看位于logs目录下的pid,然后kill pid即可;
   
2) 如果是二进制包安装的话:
首先搜搜看apt-cache search nginx
然后安装sudo apt-get install nginx
Ubuntu下查看默认安装的位置,可以使用whereis nginx
nginx和Apache类似都是通过module方式对各种功能进行扩展,关于nginx更多信息可以参看如上的链接。

3) Windows下的二进制安装非常简单:
直接下载,解压缩到C:/根目录下即可.
具体参看:http://wiki.nginx.org/NginxInstall
nginx -s [ stop | quit | reopen | reload ]

3. nginx的配置:
上面介绍了,首先nginx和Apache类似通过各种模块module可以对服务器的功能进行丰富的扩展。同样也是类似通过nginx.conf文件可以对进行核心配置或者针对各种模块的配置。
http://wiki.nginx.org/NginxModules
具体如何配置通常还是取决于,准备使用nginx来做什么,几个很好的关于配置的参考资料:
http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/
http://wiki.nginx.org/NginxConfiguration

0) 首先我们展示一下默认的nginx.conf文件中各个部分:
nginx.conf配置文件中,针对不同的模块使用大括号包括起来,很方便地进行配置;
一个非常好的教程可以参看:
http://wiki.nginx.org/NginxModules

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #这里表示设置该server的root根目录是nginx安装目录的html目录,当然可以设置绝对路径
        location / {
            root   html;
            autoindex    on; #开启自动列出目录文件功能,如果找不到index页面的话。
            index  index.html index.htm; #设置默认的首页
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        #这里是一个很好的nginx作为反向代理服务的设置,表示遇到php结尾的,会自动pass给后台侦听的apache或者别的服务器;
        #proxy the PHP scripts to Apache listening on 127.0.0.1:80
        location ~ \.php$ {
            proxy_pass   http://127.0.0.1 ;
        }
       
        #如果安装了FastCGI,则将其pass给FastCGI,这个说明了nginx本身不包裹FastCGI处理器,而是仅仅通过如下的方式pass给预先安装的FastCGI server,具体PHP的FastCGI如何安装,可以参看相关教程。
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000 ;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    #nginx的虚拟主机情况支持多种类型name-based(多个网站ip相同,域名不同),ip-based(多个网站ip不同)
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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

    #提供SSL认证的server配置
    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

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

}

下面我们来简单介绍各种使用nginx的情况:
可以参考http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/
1)默认安装的nginx配置文件中listen       80;
表示侦听80端口的HTTP请求,如果预先安装了Apache的话,就会占用该端口,从而使得Apache无法使用。
默认的配置提供对各种静态文件的访问;
2)nginx作为反向代理服务器使用
如上面的配置,
        #proxy the PHP scripts to Apache listening on 127.0.0.1:80
        location ~ \.php$ {
            proxy_pass   http://127.0.0.1 ;
        }
我们可以设置所有对php script脚本的请求,都proxy_pass到后台侦听的web server服务器(例如apache)等。
nginx还提供了一些模块例如Memcached,或者结合Squid等可以实现pass到后台web server相应之后,对返回的数据进行cache,从而提供性能。

这个相当于使用nginx作为apache的前端,可以看看这个教程:
http://sameerparwani.com/posts/nginx-as-a-front-end-to-apache/
常见的情况是,使用nginx直接处理静态的请求响应;对于动态的才pass给服务器,而且对服务器的响应结果进行缓存。

3)nginx做为多台server的负载均衡作用:
http://sameerparwani.com/posts/load-balancing-with-nginx/

4)另外一个常见的应用就是直接使用nginx代替了Apache,使用Linux+Nginx+MyQL+PHP的stack。
这个时候首先需要以FastCGI的方式安装PHP(需要一个FastCGI的process server),然后配置nginx支持PHP。
具体参考相关的专题介绍。

5)更多的配置介绍参看
http://wiki.nginx.org/NginxModules
各个模块的配置和configure教程。
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Nginx Docker安装配置

    使用Docker技术安装Nginx及配置简单的负载均衡。 将nginx的配置文件从容器中拷贝到宿主目录 $ mkdir -p /colorfulfrog/nginx/config --先在宿主机创建config目录 $ mkdir -p /colorfulfrog/nginx/html --先在宿主机...

    Ubuntu下nginx1.6和sticky1.1安装配置资料 包

    Ubuntu14.04.2下nginx1.6和sticky1.1模块的安装与简单配置,包括像文档和支持包

    nginx.conf nginx的反向代理的简单配置文件

    本资源是专门针对本博文的, nginx的反向代理的简单配置文件,给大家使用时做参考,拿走不谢,怎么一定要50字呢?

    nginx for windows下载安装与配置

    (1)nginx的特性和简介 (2)nginx for windows的下载与安装 (3)nginx处理静态资源的配置 (4)nginx 反向代理设置 (5)nginx 常见错误 引言:为什么要使用nginx 目前很多大型网站都使用了nginx,新浪、网易、QQ等都使用...

    nginx for windows下载以及详细安装与配置

    1.目前官方 Nginx 并不支持Windows,您只能在包括Linux,UNIX,BSD系统下安装和使用,现在提供nginx for windows下载以及详细安装与配置,供windows下的nginx应用。 2.Nginx 本身只是一个HTTP和反向代理服务器,它无法...

    linux下nginx安装配置相关资源

    linux下安装nginx被配置sticky模块的相关软件包,包含nginx.conf的简单实例

    nginx+tomcat多域名配置

    nginx+tomcat多域名配置 尝试过多次是比较ok!写的比较的简单有需要的朋友可以自己搭建试试做了session共享

    Nginx安装配置.rar

    有视频和简易文档,安装过程中注意每一个步骤

    Nginx一个IP如何配置多个站点的方法教程

    对于Nginx,一个IP上配置多个站点还是很常见的。尤其是在开发环境上,更是如此。 下面在我的阿里云上简单的实现这样一个需求: 在一个IP上通过对端口区分来配置多个站点。 环境初始化目录一览配置站点准备添加配置...

    nginx+Tomcat集群简单配置及相关文档

    nginx+Tomcat集群简单配置及相关文档

    openresty+nginx环境安装配置.docx

    openresty安装配置,NGINX的配置文件详解,定时清除NGINX的LOG脚本与详解,完全可以按照文件一步步搭建自己的NGINX服务,在线安装简单方便。

    nginx+tomcat负载均衡简单配置

    nginx+tomcat负载均衡简单配置

    nginx配置文件详解

    对nginx配置文件nginx.conf各个表示的意思进行解释,方便新人学习

    nginx配置注释.txt

    本文档为nginx各个配置项的简单描述,用来在使用时可以简单快速的了解

    Nginx服务器限制访问速度的配置方法

    用Nginx建站的同学,常会有限速需求。...配置简单,只需3行,打开”nginx根目录/conf/nginx.conf”配置文件修改如下: http{ …… limit_zone one $binary_remote_addr 10m; …… server { location / {

    Nginx安装入门.doc

    Nginx以事件驱动的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡服务器。在性能上,Nginx占用很少的系统资源,能支持更多的并发连接,达到更高...在安装配置上,Nginx安装简单、配置灵活。

    apache与nginx安装使用

    快速简简单的搭建一个http服务器,提供http服务,对http服务器要求不高; apache简单配置 nginx简单配置

    nginx配置反向代理

    由于服务器apache抗不住目前的并发....整个配置安装过程很简单.在考虑高并发的情况下,在安装前就做了些优化.目前配置能抗住3000以上并发.好像不是特别大哦?呵~~ 但足以~~ 只是还有少量499问题..期待有人跟我讨论解决

    nginx windows版本使用用配置

    nginx windows版本使用用配置,负载均衡配置简单。

    Nginx负载均衡与地址映射配置的完整配置

    这里已经完整的配置了nginx的地址映射和负载均衡,只需要将里面的服务改成本地的多个Tomcat的就行了。关注我 可以到2017-4-29写的博客看详情 博客名:简单认识Nginx---负载均衡

Global site tag (gtag.js) - Google Analytics