[root@hanfeng ~]# cd /usr/local/src/
[root@hanfeng src]#
[root@hanfeng src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@hanfeng src]# tar zxf nginx-1.12.1.tar.gz
[root@hanfeng src]#
[root@hanfeng src]# cd nginx-1.12.1
[root@hanfeng nginx-1.12.1]#
[root@hanfeng nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
[root@hanfeng nginx-1.12.1]# make && make install
7查看nginx目录
[root@hanfeng nginx-1.12.1]# ls /usr/local/nginx/
conf html logs sbin
[root@hanfeng nginx-1.12.1]#
[root@hanfeng nginx-1.12.1]# /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@hanfeng nginx-1.12.1]#
[root@hanfeng nginx-1.12.1]# 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@hanfeng nginx-1.12.1]# chmod 755 /etc/init.d/nginx
[root@hanfeng nginx-1.12.1]#
[root@hanfeng nginx-1.12.1]# chkconfig --add nginx
[root@hanfeng nginx-1.12.1]#
[root@hanfeng nginx-1.12.1]# chkconfig nginx on
[root@hanfeng nginx-1.12.1]#
[root@hanfeng nginx-1.12.1]# cd /usr/local/nginx/conf/
[root@hanfeng conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@hanfeng conf]# mv nginx.conf nginx.conf.1
[root@hanfeng conf]#
[root@hanfeng conf]# vim nginx.conf
在配置文件中添加以下内容
user nobody nobody; // user定义启动nginx的用户
worker_processes 2; //定义子进程有几个
error_log /usr/local/nginx/logs/nginx_error.log crit; //错误日志
pid /usr/local/nginx/logs/nginx.pid; // PID所在
worker_rlimit_nofile 51200; //nginx最多可以打开文件的上限
events
{
use epoll; //使用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 //一个server 对应一个虚拟主机,定义这个,才能正常访问网站 下面一整段,就是一个默认的虚拟主机
{
listen 80;
server_name localhost; //网站域名
index index.html index.htm index.php;
root /usr/local/nginx/html; //网站的根目录
location ~ \.php$ //配置解析php的部分
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; //nginx通过这一行配置来调用php-fpm服务
# fastcgi_pass 127.0.0.1:9000; //如果php-fpm监听的是9000端口,直接这样写配置即可
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
然后保存退出
[root@hanfeng 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
[root@hanfeng conf]#
[root@hanfeng conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 确定 ]
[root@hanfeng conf]#
[root@hanfeng conf]# ps aux |grep nginx
root 16411 0.0 0.0 20996 624 ? Ss 21:53 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 16412 0.0 0.3 23440 3212 ? S 21:53 0:00 nginx: worker process
nobody 16413 0.0 0.3 23440 3212 ? S 21:53 0:00 nginx: worker process
root 16415 0.0 0.0 112676 984 pts/0 R+ 21:55 0:00 grep --color=auto nginx
[root@hanfeng conf]#
[root@hanfeng conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@hanfeng conf]#
[root@hanfeng conf]# vim /usr/local/nginx/html/1.php
写入
<?php
echo "this is nginx test page.";
[root@hanfeng conf]# curl localhost/1.php
this is nginx test page.[root@hanfeng conf]#
server
{
listen 80 default_server; // 有这个标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
[root@hanfeng conf]# vim /usr/local/nginx/conf/nginx.conf
删除的内容
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.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@hanfeng conf]# vim /usr/local/nginx/conf/nginx.conf
增加其中的一行
application/xml;
include vhost/*.conf;
}
然后保存退出
[root@hanfeng conf]# mkdir /usr/local/nginx/conf/vhost
[root@hanfeng conf]#
[root@hanfeng conf]# cd /usr/local/nginx/conf/vhost
[root@hanfeng vhost]#
[root@hanfeng vhost]# vim aaa.com.conf
添加的文件内容
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
然后保存退出
[root@hanfeng vhost]# mkdir -p /data/wwwroot/default/
[root@hanfeng vhost]#
[root@hanfeng vhost]# cd /data/wwwroot/default/
[root@hanfeng default]#
[root@hanfeng default]# vim index.html
写入
This is the default site.
然后保存退出
[root@hanfeng default]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unexpected "}" in /usr/local/nginx/conf/nginx.conf:47
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
在后面添加 ; 即可
include vhost/*.conf;
[root@hanfeng default]# /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@hanfeng default]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng default]#
[root@hanfeng default]# curl localhost
This is the default site.
[root@hanfeng default]# curl -x127.0.0.1:80 bbb.com
This is the default site.
[root@hanfeng default]#
因为修改了nginx.conf的配置,现在看到的默认索引页,是我们刚刚新增的vhost的虚拟主机的索引页了 定义默认虚拟主机的两种办法: 1.默认虚拟主机,是根据目录的第一个.conf了进行选择,所以只需要在vhost目录下依次创建就可以了,当然这种方法不智能 2.只需要在vhost目录的.conf配置文件内,加上一个“default_server ”即可,把当前的这个配置对应的网站设置为第一个默认虚拟主机
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 /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
[root@hanfeng ~]# cd /usr/local/nginx/conf/vhost/
[root@hanfeng vhost]#
[root@hanfeng vhost]# ls
aaa.com.conf
[root@hanfeng 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 / //表示全站,都需要进行用户认证
#location /admin // 这个地方只要加上” /admin ” 就变成 针对这个站点的“admin” 这个目录需要用户认证
#location ~ admin.php //如果把这行这样写,就会变成,匹配 “ admin.php ”这个页面的时候才需要用户认证
{
auth_basic "Auth"; //定义用户认证的名字
auth_basic_user_file /usr/local/nginx/conf/htpasswd; //用户名密码文件
}
}
保存退出
/usr/local/apache2.4/bin/htpasswd
yum install -y httpd
[root@hanfeng vhost]# yum install -y httpd
在yum安装后,可以直接使用htpasswd命令
[root@hanfeng vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd hanfeng
New password: //密码hanfeng
Re-type new password:
Adding password for user hanfeng
[root@hanfeng vhost]#
[root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd
hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/
[root@hanfeng vhost]#
[root@hanfeng vhost]# htpasswd /usr/local/nginx/conf/htpasswd gurui
New password:
Re-type new password:
Adding password for user gurui
[root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd
hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/
gurui:$apr1$mqc2Dgwa$qVvurqGN6gj8hX3tEpQ6j/
[root@hanfeng vhost]#
[root@hanfeng 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@hanfeng vhost]#
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
[root@hanfeng vhost]# curl -x127.0.0.1:80 test.com
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hanfeng vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 16:52:23 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@hanfeng vhost]#
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hanfeng vhost]#
[root@hanfeng vhost]# mkdir /data/wwwroot/test.com
[root@hanfeng vhost]# echo “test.com”>/data/wwwroot/test.com/index.html
[root@hanfeng vhost]#
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com
“test.com”
[root@hanfeng vhost]#
[root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hf-01 vhost]#
[root@hf-01 vhost]# vim test.com.conf
在location / 后加上admin/ 目录
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
保存退出
[root@hf-01 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@hf-01 vhost]#
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]#
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com
“test.com”
[root@hf-01 vhost]#
6.访问test.com/admin/目录
[root@hf-01 vhost]# curl -x127.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.12.1</center>
</body>
</html>
[root@hf-01 vhost]#
[root@hf-01 vhost]# mkdir /data/wwwroot/test.com/admin
[root@hf-01 vhost]#
[root@hf-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin
in/index.html
[root@hf-01 vhost]#
[root@hf-01 vhost]# curl -x127.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.12.1</center>
</body>
</html>
[root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@hf-01 vhost]#
[root@hf-01 vhost]# vim test.com.conf
在 location 后加~ admin.php即可
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location ~ admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
保存退出
[root@hf-01 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@hf-01 vhost]#
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]#
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
test.com admin dir
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin.php
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@hf-01 vhost]#
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}
[root@hf-01 vhost]# vim test.com.conf
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}
保存退出
[root@hf-01 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@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hf-01 vhost]#
[root@hf-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 22:23:52 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
[root@hf-01 vhost]#
[root@hanfeng vhost]# curl -x127.0.0.1:80 test2.com/fgdgfdg -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 12:57:12 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/fgdgfdg
[root@hanfeng vhost]#
[root@hf-01 vhost]# curl -x127.0.0.1:80 hanfeng.com/admin/index.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 22:27:37 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@hf-01 vhost]#