一:nginx安装

(1)下载、解压 Nginx #cd /usr/local/src/ #wget http://nginx.org/download/nginx-1.8.0.tar.gz #tar zxvf nginx-1.8.0.tar.gz (2)配置编译选项 #cd nginx-1.8.0 #./configure
--prefix=/usr/local/nginx
--with-http_realip_module
--with-http_sub_module
--with-http_gzip_static_module
--with-http_stub_status_module
--with-pcre (3)编译、安装 Nginx #make #make install 因为nginx比较小,所以很快就会安装完,而且也不会出什么错误。 (4)启动nginx: #/usr/local/nginx/sbin/nginx
检查nginx是否启动: #ps aux |grep nginx 看是否有进程。

首先配置nginx配置文件,使其能够支持 php。 #vim /usr/local/nginx/conf/nginx.conf
找到 location = /50x.html { root html; } 在其后面新增如下配置: location ~ .php$ { root html; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } 重新加载 /usr/local/nginx/sbin/nginx -s reload 创建测试文件: #vim /usr/local/nginx/html/2.php 内容如下: <?php echo "test php scripts."; ?> 测试: #curl localhost/2.php test php scripts. [root@localhost nginx]# 显示成这样,才说明PHP 解析正常。

二:默认虚拟主机

#mkdir /usr/local/nginx/conf/vhosts #cd !$ #vim default.conf 加入如下配置 server { listen 80 default_server; server_name localhost; index index.html index.htm index.php; root /tmp/11111; deny all; } 说明:我们在之前的 nginx.conf中就已经定义了 include语句,意思是它会包含一些配置,在这里它会把/usr/local/nginx/conf/vhosts/目录下的所有*.conf文件加载。所以,我们在这个目录下定义了一个 default.conf文件,在这里你会发现 listen 80后面还有一个关键词叫做“default_server”,这个就是用来标记它是默认虚拟主机的。我们使用 deny all 限制了该虚拟主机禁止被任何人访问。

三:nginx用户认证

一、首先需要安装 apache,可以使用 yum install httpd 安装。然后生成密码文 件: #htpasswd -c -m /usr/local/nginx/conf/htpasswd test 这样就添加了test用户,第一次添加时需要加-c参数,第二次添加时不需要-c参数。 在nginx的虚拟主机配置文件中添加 /usr/local/nginx/vhosts/test.conf location ~ .*admin.php$ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } 这样就会把请求/uc_server/的访问给限制了,只有输入用户名和密码才可以继续访问, 基本上和apache的配置类似。 location /uc_server/ { }:针对文件夹时,输入php文件所在路径

四:nginx域名重定向

在虚拟主机vhosts路径下配置文件中配置如下: 在server_name后面添加需要跳转的域名 if ($host != 'www.a.com' ) { rewrite ^/(.*)$ http://www.a.com/$1 permanent; } 和 apache的相关配置很像。 301跳转设置:

server { listen 80; server_name 123.com; rewrite ^/(.*) http://456.com/$1 permanent; access_log off; }

302跳转设置:

server { listen 80; server_name 123.com; rewrite ^/(.*) http://456.com/$1 redirect; access_log off; }