Nginx安装,默认虚拟主机以及认证和重定向
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx安装,默认虚拟主机以及认证和重定向相关的知识,希望对你有一定的参考价值。
nginx安装
1.首先下载安装包
[[email protected] src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
--2018-03-14 00:46:57-- http://nginx.org/download/nginx-1.12.2.tar.gz
正在解析主机 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
正在连接 nginx.org (nginx.org)|206.251.255.63|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
2.解压缩
[[email protected] src]# tar zxf nginx-1.12.2.tar.gz
[[email protected] src]# cd nginx-1.12.2/
3.编译
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
4.接下来安装:
make
make install
5.安装完成:
[[email protected] nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
6.制作启动脚本:
[[email protected] nginx-1.12.2]# 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=$?
7.更改权限:
[[email protected] nginx-1.12.2]# chmod 755 /etc/init.d/nginx
[[email protected] nginx-1.12.2]# chkconfig --add nginx
[[email protected] nginx-1.12.2]# chkconfig nginx on
8.更改配置文件:
[[email protected] 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;
9.启动服务:
[[email protected] conf]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 确定 ]
[[email protected] conf]# ps aux |grep nginx
root 4772 0.0 0.0 20496 624 ? Ss 01:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 4773 0.0 0.3 22940 3212 ? S 01:10 0:00 nginx: worker process
nobody 4774 0.0 0.3 22940 3212 ? S 01:10 0:00 nginx: worker process
root 4776 0.0 0.0 112676 984 pts/0 R+ 01:11 0:00 grep --color=auto nginx
Nginx默认虚拟主机
1.首先修改配置文件:
[[email protected] conf]# vim nginx.conf
application/xml;
include vhost/*.conf;
}
~
2.创建默认主机:
[[email protected] conf]# mkdir vhost
[[email protected] conf]# cd vhost/
[[email protected] vhost]# ls
[[email protected] 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;
}
3.测试并重新加载:
[[email protected] 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
[[email protected] default]# /usr/local/nginx/sbin/nginx -s reload
4.验证:
[[email protected] default]# curl localhost
This is the default site
用户认证
1.做虚拟主机配置文件:
[[email protected] 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;
}
}
2.生成密码文件:
[[email protected] vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd weixing
New password:
Re-type new password:
Adding password for user weixing
[[email protected] vhost]# cat /usr/local/nginx/conf/htpasswd
weixing:$apr1$bCpQS9At$I6X83gZ8dJ2f1Z3.e1y.c1
3.测试,重新加载:
[[email protected] 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
[[email protected] vhost]# /usr/local/nginx/sbin/nginx -s reload
4.验证:
[[email protected] 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.2</center>
</body>
</html>
[[email protected] vhost]# curl -u weixing:3914 -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.2</center>
</body>
</html>
创建目录后重新测试:
[[email protected] vhost]# ls /data/wwwroot/test.com
ls: 无法访问/data/wwwroot/test.com: 没有那个文件或目录
[[email protected] vhost]# mkdir /data/wwwroot/test.com
[[email protected] vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[[email protected] vhost]# curl -u weixing:3914 -x127.0.0.1:80 test.com
test.com
Nginx域名重定向
1.修改配置文件:
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;
}
location /
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
2.测试:
[[email protected] vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Tue, 13 Mar 2018 17:53:50 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html
以上是关于Nginx安装,默认虚拟主机以及认证和重定向的主要内容,如果未能解决你的问题,请参考以下文章
Nginx安装 默认虚拟主机 Nginx用户认证 Nginx域名重定向
nginx安装默认虚拟主机nginx用户认证nginx域名重定向
47.Nginx安装默认虚拟主机Nginx用户认证Nginx域名重定向