Nginx 安装
-
下载并解压包
下载Nginx:
[root@localhost src]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
解压Nginx
[root@localhost src]# tar -zxv -f nginx-1.8.0.tar.gz
-
配置Nginx:
切换目录:
[root@localhost src]# cd nginx-1.8.0
配置:
[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx
#如果需要支持某模块,可以在此添加,如HTTPS、SSL等
......
[root@localhost nginx-1.8.0]# echo $?
0
编译和安装:
[root@localhost nginx-1.8.0]# make
[root@localhost nginx-1.8.0]# echo $?
0
[root@localhost nginx-1.8.0]# make install
[root@localhost nginx-1.8.0]# echo $?
0
查看安装目录:
[root@localhost nginx-1.8.0]# ls /usr/local/nginx/
conf html logs sbin
-
创建启动脚本
在/etc/init.d/目录下创建nginx文件,并添加如下内容:
[root@localhost nginx-1.8.0]# vim /etc/init.d/nginx
#!/bin/bash
chkconfig: - 30 21
description: http service.
Source Function Library
. /etc/init.d/functions
Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
更改权限:
[root@localhost nginx-1.8.0]# chmod 755 /etc/init.d/nginx
设置nginx开机启动
[root@localhost nginx-1.8.0]# chkconfig --add nginx
[root@localhost nginx-1.8.0]# chkconfig nginx on
[root@localhost nginx-1.8.0]#
-
更改配置文件
[root@localhost nginx-1.8.0]# cd /usr/local/nginx/conf/
[root@localhost conf]# mv nginx.conf nginx.conf.bak
[root@localhost conf]# vim nginx.conf
user nobody nobody;
#定义启动Nginx进程的用户
worker_processes 2;
#定义子进程数目
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
#指定Nginx最多可打开的文件数目
events
{
use epoll;
worker_connections 6000;
#进程最大连接数
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
#虚拟主机
{
listen 80;
server_name localhost;
#服务名称
index index.html index.htm index.php;
root /usr/local/nginx/html;
#php文件路径
location ~ .php$
#配置PHP解析
{
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;
}
}
}
检查配置文件是否有错:
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动nginx服务:
[root@localhost conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 确定 ]
测试nginx解析php:
[root@localhost conf]# vim usr/local/nginx/html/1.php
<?php
echo "this is nginx test page";
[root@localhost conf]# curl localhost/1.php
this is nginx test page
Nginx 默认虚拟主机
编辑nginx配置文件:
[root@localhost conf]
......
将server部分去掉。
添加下面这行:
include vhost/*.conf;
创建vhost目录:
[root@localhost conf]
在vhost目录下创建一台虚拟主机:
[root@localhost vhost]
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
创建配置文件中root指定的目录
[root@localhost vhost]
添加索引页:
[root@localhost vhost]
This is the default directory.
检测配置文件:
[root@localhost vhost]
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重载配置文件:
[root@localhost vhost]
检测:
[root@localhost vhost]
This is the default directory.
Nginx 用户认证
-
创建一台虚拟主机
[root@localhost vhost]# vim test.com.conf
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
#指定设置用户认证的目录
{
auth_basic "Auth";
#指定用户名
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
#指定用户的密码文件
}
}
注意: 上述“location”中的内容即为设定用户认证。在此是为整个站点设定的用户认证,如果只是为某个目录设置用户认证,在location所在行进行编辑就好,如:location /admin 目录。也可以对某种请求(即对一个普通文件)设定用户认证,如location ~ admin.php()使用 ~ 进行匹配)
-
创建密码文件:
在此需要使用Apache的/usr/local/apache/bin/htpasswd命令,如果机器中已经有Apache,可以直接使用,如果没有,需要使用yum安装httpd命令。
安装httpd:
[root@localhost vhost]# yum install -y httpd
创建密码文件:
[root@localhost vhost]# htpasswd -c -m /usr/local/nginx/conf/htpasswd huang
New password:
Re-type new password:
Adding password for user huang
创建密码文件htpasswd,指定用户为黄。‘-c’=create,创建该密码文件,如果是第二次添加用户,不用加该选项,所添加的用户名和密码会保存到该文件下。-m 使用md5加密文件。
-
检测并重载
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
注意: 使用reload而不使用restart的好处是能避免因配置文件中存在错误而无法正常启动!当配置文件有错误时reload是不会生效的,也就是说不会破坏原来的nginx服务。
-
添加指定目录并测试
添加虚拟主机中指定的目录:
[root@localhost vhost]# mkdir /data/wwwroot/test.com
[root@localhost vhost]# vim /data/wwwroot/test.com/index.html
test.com
测试:
[root@localhost vhost]# curl -uhuang:159820 -x 127.0.0.1:80 test.com
test.com
-
定义访问目录用户认证:
编辑虚拟主机配置文件:
[root@localhost vhost]# vim test.com.conf
......
location /admin/ #指定用户认证的目录
......
创建目录:
[root@localhost vhost]# mkdir /data/wwwroot/test.com/admin/
[root@localhost vhost]# vim /data/wwwroot/test.com/admin/index.html
test.com admin dir
检测并重载配置文件:
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
测试:
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com
访问test.com不需要用户认证直接访问
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
[root@localhost vhost]# curl -uhuang:159820 -x 127.0.0.1:80 test.com/admin/
test.com admin dir
访问test.com/admin/需要用户认证。
-
针对url用户认证
修改虚拟主机中location:
location ~ admin.php
访问test.com和admin不需要用户认证,访问admin.php 需要用户认证
Nginx域名重定向
编辑虚拟主机配置文件:
[root@localhost vhost]
server
{
listen 80;
server_name test.com test2.com test3.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
检测并重载配置:
[root@localhost vhost]
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost vhost]
测试:
[root@localhost vhost]
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 03 Jan 2018 15:37:44 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html
http://blog.51cto.com/754599082/2057215