大鸟的学习乐园
路漫漫其修远兮,吾将上下而求索
posts - 26,comments - 27,trackbacks - 0

h1. Nginx编译,安装配置

 

* Nginx

Apache知道吧,Nginx和他一也是webserver,不他比Apache快,据快很多很多,尤其是在高荷的候。

BTW,玩意是某俄国大牛一个人写的......

 

* 编译

./configure --prefix=/usr/local/nginx

make && make install

 

* 配置虚主机

/usr/local/nginx/conf/nginx.conf 内容如下

<pre>

user www;

worker_processes  1;

events {

    worker_connections  1024;

}

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

 

    # [Add by lemon] Include vhost config

    include /usr/local/nginx/conf/vhost/www_site1_com.conf;

    include /usr/local/nginx/conf/vhost/www_site2_com.conf;

</pre>

/usr/local/nginx/conf/vhost/www_site1_com.conf 内容如下

<pre>

server {

    listen 192.168.188.132:80;

    client_max_body_size 100M;

    server_name  www.site1.com;

    #charset gb2312;

    index index.html index.htm index.php;

    root   /home/www/site1;         #你的站点路径

 

    #录浏览这样当没有找到index文件,就也已浏览中的文件

 

    autoindex  on;

 

    if (-d $request_filename) {

    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;

    }

 

    error_page  404              /404.html;

    location = /40x.html {

    root  /home/www/site1;       #你的站点路径

    charset    on;

    }

 

    # redirect server error pages to the  static page /50x.html

 

    #

 

    error_page   500 502 503 504  /50x.html;

 

    location = /50x.html {

    root   /home/www/site1;      #你的站点路径

    charset    on;

    }

 

    #将客端的fastcgi

 

    location ~ .*\.(php|php5|php4|shtml|xhtml|phtml)?$ {

 

    fastcgi_pass   127.0.0.1:9000;

 

    include /usr/local/nginx/conf/fastcgi_params;

 

    }

 

    #网站的多,更改少,将它浏览器本地15

 

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

 

    {

 

    expires      15d;

 

    }

 

    #网站会加很多JSCSS,将它浏览器本地1

 

    location ~ .*\.(js|css)?$

    {

    expires      1d;

    }

 

    location /(WEB-INF)/ {

    deny all;

    }

 

    #定日志格式

 

    log_format  site1_access  '$remote_addr - $remote_user [$time_local] "$request" '

 

    '$status $body_bytes_sent "$http_referer" '

 

    ' "$http_user_agent" $http_x_forwarded_for';

 

    #定本虚主机的访问日志

 

    access_log  /home/www/site1/logs/nginx/access.log  site1_access;   #日志的路径,个虚机一个,不能相同

 

    #防止nginxweb,server_name问题.击这看原文

    server_name_in_redirect  off;

}

</pre>

/usr/local/nginx/conf/vhost/www_site2_com.conf www_site1_com.conf 基本一,只需把site1site2即可。

 

* Nginx+PHP

Nginx 只是一个http器,本身不能php但它可以通fastcgiphp

php内置了一个fastcgi server 需要通php-fpm来启个在编译php需要指定参数,以cgi模式编译

所以,Nginx只要配置把php求交fastcgi server的部分即可,剩下的事情交 fastcig server去做。

<pre>

location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;

            include        fastcgi_params;

        }

</pre>

/usr/local/nginx/html/NginxDocumentRoot

 

* 主机与IP访问共存

"配置虚主机"中介的方式配置虚主机以后,会发现如果用http://<ip>:<port>/xxx的方式无法访问DocumentRoot/usr/local/nginx/html)下的用。

解决方法:

/usr/local/nginx/conf/vhost/下建立一个新的配置文件 localhost.conf(可以copy其他的vhost配置文件),要改的地方如下

vim /usr/local/nginx/conf/vhost/ localhost.conf

<pre>

……

server_name  localhost;

……

root   /usr/local/nginx/html;

……

</pre>

如果需要支持PHP

<pre>

location ~ .*\.(php|php5|php4|shtml|xhtml|phtml)?$ {

 

fastcgi_pass   127.0.0.1:9000;

 

fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;

 

include /usr/local/nginx/conf/fastcgi_params;

 

}

……

</pre>

posted on 2012-01-18 21:45 大鸟 阅读(480) 评论(0)  编辑  收藏 所属分类: linux

只有注册用户登录后才能发表评论。


网站导航: