linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍
Nginx介绍
- Nginx官网(http://nginx.org),最新版1.13,最新稳定版1.12
- Nginx应用场景:web服务、反向代理、负载均衡
- Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并
- Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考 http://jinnianshilongnian.iteye.com/blog/2280928
Nginx安装
1. 进入存放源码包目录
[root@gary-tao ~]# cd /usr/local/src/
[root@gary-tao src]#
2. 下载源码包
[root@gary-tao src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
3. 解压压缩包
[root@gary-tao src]# tar zxf nginx-1.12.1.tar.gz
4. 安装nginx
[root@gary-tao src]# cd nginx-1.12.1
[root@gary-tao nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
5. 编译nginx
[root@gary-tao nginx-1.12.1]# make
[root@gary-tao nginx-1.12.1]# make install
[root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/ //查看nginx核心配置文件
conf html logs sbin
[root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/conf/
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/html/
50x.html index.html
[root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/logs/
[root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/sbin/
nginx
[root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx
[root@gary-tao 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
6. 创建nginx配置文件及编辑启动脚本
[root@gary-tao 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_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL
}
stop()
{echo -n $"Stopping $prog: "killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL
}
reload()
{echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL
}
restart()
{stopstart
}
configtest()
{$NGINX_SBIN -c $NGINX_CONF -treturn 0
}
case "$1" instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1
esac
exit $RETVAL
7.修改启动脚本权限
[root@gary-tao nginx-1.12.1]# chmod 755 /etc/init.d/nginx
8. 添加nginx服务
[root@gary-tao nginx-1.12.1]# chkconfig --add nginx
9. 设置开机启动
[root@gary-tao nginx-1.12.1]# chkconfig nginx on
10. 配置nginx的配置文件
[root@gary-tao nginx-1.12.1]# cd /usr/local/nginx/conf/
[root@gary-tao conf]# ls
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@gary-tao conf]# mv nginx.conf nginx.conf.bak
[root@gary-tao conf]# vim nginx.conf配置如下内容:user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
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;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;} }
}
11. 测试配置文件语法
[root@gary-tao 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
12. 开启Nginx
[root@gary-tao conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.[ 确定 ]
[root@gary-tao conf]# systemctl daemon-reload //解决上面启动时报错
[root@gary-tao conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 确定 ]
13. 查询nginx启动界面
[root@gary-tao conf]# ps aux |grep nginx
root 62748 0.0 0.0 20500 624 ? Ss 18:51 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 62749 0.0 0.3 22944 3212 ? S 18:51 0:00 nginx: worker process
nobody 62750 0.0 0.3 22944 3212 ? S 18:51 0:00 nginx: worker process
root 62835 0.0 0.0 112680 976 pts/1 S+ 19:02 0:00 grep --color=auto nginx
[root@gary-tao 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@gary-tao conf]# ls /usr/local/nginx/html/ //页面文件目录
50x.html index.html
14.测试Nginx解析php
[root@gary-tao conf]# vim /usr/local/nginx/html/1.php 配置如下内容:<?php
echo "This is nginx test page.";
?>
使用curl测试
root@gary-tao conf]# curl localhost/1.php
This is nginx test page.[root@gary-tao conf]#
[root@gary-tao conf]#
Nginx默认虚拟主机
在Nginx中也有默认虚拟主机,跟httpd类似,第一个被Nginx加载的虚拟主机就是默认主机,但和httpd不相同的地方是,它还有一个配置用来标记默认虚拟主机,也就是说,如果没有这个标记,第一个虚拟主机为默认虚拟主机。
1. 编辑配置文件:
vim /usr/local/nginx/conf/nginx.conf 增加以下内容:include vhost/*.conf;
如图:
2. 创建一个vhost目录
[root@gary-tao conf]# mkdir /usr/local/nginx/conf/vhost
[root@gary-tao conf]# cd /usr/local/nginx/conf/vhost/
[root@gary-tao vhost]# vim aaa.conf //进入vhost目录下并创建编辑一个.conf文件增加如下内容:server
{listen 80 default_server; // 有这个标记的就是默认虚拟主机server_name aaa.com;index index.html index.htm index.php;root /data/wwwroot/default;
}
3.创建default目录
[root@gary-tao vhost]# mkdir /data/wwwroot/default
[root@gary-tao vhost]# cd /data/wwwroot/default/
[root@gary-tao default]# ls
[root@gary-tao default]# vim index.html在default目录下的index.html文件中定义如下内容:This is the default site.
4.测试语法,重新加载配置文件(不需要重启服务)
[root@gary-tao 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@gary-tao default]# /usr/local/nginx/sbin/nginx -s reload //重新加载配置
5.使用curl测试
[root@gary-tao default]# curl localhost
This is the default site.
[root@gary-tao default]# curl -x127.0.0.1:80 123.com
This is the default site.
[root@gary-tao default]# curl -x127.0.0.1:80 aaaa.com
This is the default site.
[root@gary-tao default]# curl -x127.0.0.1:80 ddd.com
This is the default site.
解释说明:
访问的域名无论是指定的aaa.com还是其它域名,只要解析过来,指向到我们服务器,都能访问到这个站点,这就是默认虚拟主机。
Nginx用户认证
1. 创建一个虚拟主机配置文件
[root@gary-tao conf]# vim /usr/local/nginx/conf/vhost/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;
}
}
2.创建密码文件
[root@gary-tao conf]# yum install -y httpd //如果之前没有安装过Apache的话就安装httpd,是为了可以使用Apache的htpasswd工具创建用户
[root@gary-tao conf]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming
备注:Apache自带命令htpasswd创建密码文件,-c是创建,-m是指定md5加密类型,指定用户为aming(备注:如果再次新增用户,就不需要再加 -c ,因为已经创建过密码文件了,加了会覆盖之前的创建好的用户)
New password:
Re-type new password:
Adding password for user aming
[root@gary-tao src]# cat /usr/local/nginx/conf/htpasswd
aming:$apr1$o3zygnex$U.fWNEyk7.OeiwK.hcsBw/
[root@gary-tao src]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd xie
New password:
Re-type new password:
Adding password for user xie
[root@gary-tao src]# cat /usr/local/nginx/conf/htpasswd
aming:$apr1$o3zygnex$U.fWNEyk7.OeiwK.hcsBw/
xie:$apr1$rBHXFLAp$fx9B2QPOtVQzLnz5hIWme1
3.测试语法及重新加载配置
[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload
4.使用curl命令测试用户认证
[root@gary-tao src]# 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@gary-tao src]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 13:01:05 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@gary-tao src]# curl -x127.0.0.1:80 -uaming:aming test.com -I //增加用户访问
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 13:01:36 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@gary-tao src]# mkdir /data/wwwroot/test.com //创建用户目录
[root@gary-tao src]# echo “test.com”>/data/wwwroot/test.com/index.html //在test.com目录下编辑index.html
[root@gary-tao src]# curl -x127.0.0.1:80 -uaming:aming test.com
“test.com”
针对目录的用户认证
[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf //进入配置文件,在location后面加上目录名字就可以
如图:
使用curl测试:
[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload
[root@gary-tao src]# curl -x127.0.0.1:80 test.com //访问网站正常
“test.com”
[root@gary-tao src]# curl -x127.0.0.1:80 test.com/admin/ //但是访问admin目录下时就会出现401,需要用户认证
<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@gary-tao src]# mkdir /data/wwwroot/test.com/admin //创建目录地址
[root@gary-tao src]# echo "test.com adming dir" > /data/wwwroot/test.com/admin/index.html
[root@gary-tao src]# 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@gary-tao src]# curl -x127.0.0.1:80 -uaming:aming test.com/admin/ //使用用户密码访问正常
test.com adming dir
针对文件的用户认证
[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf //进入配置文件,在location后面加上匹配文件名字
如图:
使用curl测试,访问admin目录时是正常的,访问admin.php就需要用户认证了
[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload
[root@gary-tao src]# curl -x127.0.0.1:80 test.com/admin/
test.com adming dir
[root@gary-tao src]# 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>
Nginx域名重定向
1. 更改test.com.conf
[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf定义如下内容:server
{listen 80;server_name test.com test1.com test2.com;
//server_name后面支持写多个域名,这里要和httpd的做一个对比index index.html index.htm index.php;root /data/wwwroot/test.com;if ($host != 'test.com' ) {rewrite ^/(.*)$ http://test.com/$1 permanent;
//permanent为永久重定向,状态码为301,如果写redirect则为302}
}
如图:
2. 测试语法及重新加载配置,使用curl测试
[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload
[root@gary-tao src]# curl -x127.0.0.1:80 test2.com/index.html -I //访问test2.com后会跳转到test.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 13:30:03 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html[root@gary-tao src]# curl -x127.0.0.1:80 test2.com/index.html/adgagadga -I //访问test2.com后会跳转到test.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 13:30:21 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html/adgagadga[root@gary-tao src]# curl -x127.0.0.1:80 test3.com/index.html/adgagadga -I //访问test3.com就跳转到默认虚拟主机,报错404
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 13:30:38 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
转载于:https://blog.51cto.com/taoxie/2057175
linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍相关推荐
- 11-4 12 Nginx安装 默认虚拟主机 用户认证 域名重定向
2019独角兽企业重金招聘Python工程师标准>>> 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向 扩展 ngin ...
- 12.6-12.9 Nginx安装,默认虚拟主机,用户认证,域名重定向
12.6 Nginx安装 大纲 1 进入src目录,把nginx下载在此目录 #cd /usr/local/src #wget http://nginx.org/download/nginx-1.8 ...
- Nginx安装与虚拟主机配置shell脚本
今天继续给大家介绍Linux运维相关知识,本文主要内容是Nginx安装与虚拟主机配置shell脚本. 一.NGINX安装 在今天的脚本上,我们开始尝试使用函数和参数,以增强脚本的灵活性和实现脚本的模块 ...
- Nginx——安装与虚拟主机配置(域名非80端口问题)
一.引言 因为甲方提出问题:一个服务器与IP,两个域名a.b分别对应两个要部署的网站,这个一般是一个域名a对应一个主机服务器ip的默认80端口,另外的网站只能用其他端口(如b:8080对应主机ip:8 ...
- Nginx安装、默认虚拟主机、Nginx用户认证、Nginx域名重定向
12.6 Nginx安装 安装包下载到/usr/local/src目录 [root@taoyuan ~]# cd /usr/local/src [root@taoyuan src]# wget htt ...
- linux基于域名的虚拟主机,Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机...
Nginx支持的虚拟主机有三种 ●基于域名的虚拟主机 ●基于IP的虚拟主机 ●基于端口的虚拟主机 每一种虚拟主机均可通过"server{}" 配置段实现各自的功能 基于域名的虚拟主 ...
- Nginx优化之虚拟主机
文章目录 前言 一:Nginx服务基础 1.1:Nginx概述 1.2:Nginx编译安装(过程) 1.3:运行控制(实验过程) 1.4:配置文件nginx.conf 1.5:Nginx的访问状态统计 ...
- 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx
12.6 Nginx安装 [root@martin001 conf]# chkconfig --add nginx [root@martin001 conf]# chkconfig nginx on ...
- php nginx 域名重定向,Nginx默认虚拟主机、用户认证、域名重定向
Nginx默认虚拟主机定义默认虚拟主机配置文件,在http下面加入include vhost/*.conf 在/usr/local/nginx/conf/下创建目录 #mkdir vhost/ //创 ...
最新文章
- 少女时代成为主流:这是一件好事吗?
- python苹果手机的api_Python中的api,API
- Office安装时报错1907的解决方法
- 用自然语言从GitHub搜代码,跳过论坛提问环节,来自Facebook新研究
- 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析
- Appium base knowledge
- Linux有关Shell算数运算的用法补充笔记
- 10个最常见的JS错误
- 全链路压测平台(Quake)在美团中的实践
- 安装Ms SQL Server 2005 开发版时出现性能计数器要求安装错误的解决办法
- Java即时编译器JIT之简单介绍
- ACM 竞赛高校联盟 练习赛 第六场 光头强的强迫症(线段树)
- Voltage Keepsake CodeForces - 801C(二分)
- 内网/外网介绍以及两者比较
- yarn : 无法加载文件 C:\Users\L\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
- 你真的认为iPhone只是一部手机?苹果惊天秘密之 四
- Python数据分析之股票数据
- 感悟和体会数据结构和算法
- python使用pd.to_numeric()方法将数据转为数字类型int或float
- 电脑电量为0,显示已接通,却充不上电
热门文章
- python绘制饼图的列表有none_python 有没有库或者函数,类似R的corrplot,画相关系数图的时候,每个方框里面为饼图?...
- activexobject对象不能创建_【设计模式】建造者模式:你创建对象的方式有它丝滑吗?...
- RuntimeException: Package manager has died
- 第四篇:mysql管理工具
- vue 如何生成一个dom元素_vue 学习心得——DOM树如何被构建
- Linux初学(CnetOS7 Linux)之切换命令模式和图形模式的方法
- LoadRunner 测试Oracle数据库及Siebel性能
- 在 Linux 命令行中使用 tcpdump 抓包
- 洛谷P1091 合唱队形
- 关于UC、火狐、谷歌浏览器屏蔽布局中广告的解决办法