h1. Nginx編譯,安裝配置
* Nginx是啥?
Apache知道吧,Nginx和他一樣也是webserver,不過他比Apache快,據(jù)說快很多很多,尤其是在高負(fù)荷的時候。
BTW,這玩意是某俄國大牛一個人寫的......
* 編譯
./configure --prefix=/usr/local/nginx
make && make install
* 配置虛擬主機(jī)
/usr/local/nginx/conf/nginx.conf 內(nèi)容如下
<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 內(nèi)容如下
<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; #你的站點(diǎn)路徑
#打開目錄瀏覽,這樣當(dāng)沒有找到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; #你的站點(diǎn)路徑
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; #你的站點(diǎn)路徑
charset on;
}
#將客戶端的請求轉(zhuǎn)交給fastcgi
location ~ .*\.(php|php5|php4|shtml|xhtml|phtml)?$ {
fastcgi_pass 127.0.0.1:9000;
include /usr/local/nginx/conf/fastcgi_params;
}
#網(wǎng)站的圖片較多,更改較少,將它們在瀏覽器本地緩存15天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 15d;
}
#網(wǎng)站會加載很多JS、CSS,將它們在瀏覽器本地緩存1天
location ~ .*\.(js|css)?$
{
expires 1d;
}
location /(WEB-INF)/ {
deny all;
}
#設(shè)定日志格式
log_format site1_access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
' "$http_user_agent" $http_x_forwarded_for';
#設(shè)定本虛擬主機(jī)的訪問日志
access_log /home/www/site1/logs/nginx/access.log site1_access; #日志的路徑,每個虛擬機(jī)一個,不能相同
#防止nginx做web服務(wù)的時候,多server_name的問題.點(diǎn)擊這里查看原文
server_name_in_redirect off;
}
</pre>
/usr/local/nginx/conf/vhost/www_site2_com.conf 與www_site1_com.conf 基本一樣,只需把site1替換成site2即可。
* Nginx+PHP
Nginx 只是一個http服務(wù)器,本身不能處理php。但它可以通過fastcgi調(diào)用php。
php內(nèi)置了一個fastcgi server, 需要通過php-fpm來啟動,這個在編譯php時需要指定參數(shù),以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/是Nginx默認(rèn)的DocumentRoot
* 虛擬主機(jī)與IP訪問共存
按"配置虛擬主機(jī)"中介紹的方式配置虛擬主機(jī)以后,會發(fā)現(xiàn)如果用http://<ip>:<port>/xxx的方式無法訪問DocumentRoot(/usr/local/nginx/html)下的應(yīng)用。
解決方法:
在/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>