nginx 在 centos 环境下安装
NGINX安装,最新下载,配置,优化等
下载nginx。
下载 后,编译。
tar zxvf nginx-0.7.66.tar.gz
cd nginx-0.7.66
./configure –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_stub_status_module –with-http_ssl_module
make
make install
然后先不要启动nginx,对conf文件做一些修改,主要是对缓存部分的设置。
vi /usr/local/nginx/conf/nginx.conf
里面对缓存的设置部分如下。写在http段里
proxy_temp_path /usr/local/nginx/proxy_temp;
proxy_cache_path /usr/local/nginx/proxycache levels=1:2 keys_zone=CachePool:300m inactive=1d max_size=3g;
server {
listen 真实的公网IP:80;
server_name localhost;
location / {
proxy_pass http://127.0.0.2;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header FORWARDED-FOR $remote_addr;
proxy_set_header Host $host;
location ~* \.(js|css|gif|png|bmp|jpeg|jpg|swf)$ {
proxy_pass http://127.0.0.2;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_cache CachePool;
#proxy_cache_key $host$request_uri$http_if_modified_since;
proxy_cache_key $host$request_uri;
proxy_cache_valid 5m;
}
}
一些解释
max_size=3g 缓存占用的最大空间3G
proxy_pass http://127.0.0.2 回头我们要把apache的监听改成监听这个IP的80端口。当然,也可以用其他端口和其他IP,这个自由设置。
proxy_cache_valid 5m; 缓存生存时间5分钟。这个时间的设置也要根据主机上的站点来具体设置,没有任何可以作为标准的说法。
设置完后,先停止 httpd。
/etc/init.d/httpd stop
/etc/init.d/directadmin stop
directadmin 也一定是要停的,不然会自动启动httpd进程。
修改文件
/etc/httpd/conf/extra/httpd-vhosts.conf
对应添加修改成
LogFormat “%O \”%r\”" homedir
NameVirtualHost 127.0.0.2:80
NameVirtualHost 真实公网IP:443
ServerAdmin webmaster@localhost
AliasMatch ^/~([^/]+)(/.*)* /home/$1/public_html$2
DocumentRoot /var/www/html
ServerName localhost
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
CustomLog /var/log/httpd/homedir.log homedir
然 后安装一个apache需要的小模块。
wget http://www.openinfo.co.uk/apache/extract_forwarded-2.0.2.tar.gz
tar zxvf extract_forwarded-2.0.2.tar.gz
cd extract_forwarded
apxs -c -i -a mod_extract_forwarded.c
结束后修改/etc/httpd/conf/httpd.conf
ServerRoot “/etc/httpd”
Listen 127.0.0.2:80
#LoadModule dummy_module /usr/lib/apache/mod_dummy.so
#LoadModule php5_module /usr/lib/apache/libphp5.so
LoadModule extract_forwarded_module /usr/lib/apache/mod_extract_forwarded.so
MEForder refuse,accept
MEFrefuse all
MEFaccept 127.0.0.2
Include /etc/httpd/conf/extra/httpd-phpmodules.conf
FileETag none
User apache
Group apache
ServerAdmin admin@localhost
DocumentRoot “/var/www/html”
接下来是修改directadmin里已经建立的用户的对应配置。
cd /usr/local/directadmin/data/users
find -name ‘*.conf’ | xargs perl -pi -e ’s|真实公网IP|127.0.0.2|g’
然后修改directadmin的模板,让以后新添加的用户也自动启用对应的设置。
进 入/usr/local/directadmin/data/templates/custom 自定义模板的地方
里面需要从上层目录拷贝6个 文件进来。
ips_virtual_host.conf
virtual_host2.conf
virtual_host.conf
redirect_virtual_host.conf
virtual_host2_sub.conf
virtual_host_sub.conf
然后对应修改这六个文件中的部 分,主要就是类似
然后就可 以了,检查确认无误。
/etc/init.d/httpd start
/usr/local/nginx/sbin/nginx (启动nginx)
检查进程,里面已经有nginx和httpd同时在跑了。初次的缓存生成后,用户再次读取静态文件的时候,就不需要麻烦 apache去了。
nginx的高级设置,这个就另外自行查看资料了。
对于多个域名多个IP的服务器,有时候我们需要用户不能通过的IP直接访问网站。这有的时候是因为IDC机房的需要,有的时候是我们为了将网站直接隔绝开的需要。本文是想讨论Nginx下如何实现这样的设置。
如果是Apache的话比较好设定,可以用NameVirtualHost来指定哪个IP绑定哪个域名,但是nginx应该如何把ip绑定到域名商呢?Slicehost论坛上也有人讨论过如何绑定ip的问题。核心的问题就是,如果直接输入ip,nginx会把这个ip对应到哪个域名上呢?答案是随机的。如果想要在某个域名做为catchall的,可以设定如下规则:
server{
listen 80 default;
....
}
那么如何绑定独立IP呢?由于ip信息其实是在在NGINX官方HTTPcore Listen的说明文档上也提到了这个是用方法,可以比较方便的是使用如下
server{
listen 99.69.16.3:80;
server_name www.inginx.com
....
}
也可以直接这样写:
server{
listen 114.163.186.110;
server_name www.abc.com
....
}
server{
listen 99.69.16.3;
server_name www.abe.com
....
}
这样独立的ip就被绑定到固定的域名上去了。
因为最近作者指出nginx一直以来都存在一个安全漏洞,因此今天升级了nginx
[root@unixhater ~]# cd /opt/nginx/
[root@unixhater nginx]# sbin/nginx -V #查看版本
nginx version: nginx/0.7.61
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-44)
configure arguments: –user=www –group=www –prefix=/opt/nginx –with-http_stub_status_module –with-http_ssl_module #注意这里的编译项
[root@unixhater nginx]# wget http://sysoev.ru/nginx/nginx-0.7.62.tar.gz #下载
[root@unixhater nginx]# tar xzvf nginx-0.7.62.tar.gz #解压缩
[root@unixhater nginx]# cd nginx-0.7.62
[root@unixhater nginx-0.7.62]# ./configure –user=www –group=www –prefix=/opt/nginx –with-http_stub_status_module –with-http_ssl_module #按原来的选项configure
[root@unixhater nginx-0.7.62]# make #编译
[root@unixhater nginx-0.7.62]# mv /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.old #移动旧版本
[root@unixhater nginx-0.7.62]# cp objs/nginx /opt/nginx/sbin/ #复制新版本nginx过去
[root@unixhater nginx-0.7.62]# make upgrade #无缝升级,当前连接不会断
[root@unixhater nginx-0.7.62]# cd ..
[root@unixhater nginx]# sbin/nginx -V
nginx version: nginx/0.7.62
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-44)
configure arguments: –user=www –group=www –prefix=/opt/nginx –with-http_stub_status_module –with-http_ssl_module
[root@unixhater nginx]# rm -rf nginx-0.7.62 nginx-0.7.62.tar.gz #清理
location /private/ {
allow 192.168.1.0/24;
deny all;
}
location ~ ^/private/.*\.php$ {
allow 192.168.1.0/24;
deny all;
include conf/enable_php5.conf;
}
location ~ \.php$ {
include conf/enable_php5.conf;
}
这样就可以实现目录private只允许192.168.1.0/24的访问了