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

原文地址:http://blog.51cto.com/13517254/2086297

时间: 2025-01-18 06:47:29

Nginx安装,默认虚拟主机以及认证和重定向的相关文章

nginx安装,虚拟主机,用户认证及域名重定向

nginx安装 cd /usr/local/src/ wget http://nginx.org/download/nginx-1.14.0.tar.gz tar zxfv nginx-1.14.0.tar.gz cd nginx-1.14.0/ ./configure --prefix=/usr/local/nginx make && make install 启动文件配置vim /etc/init.d/nginx,参考下面 #!/bin/bash # chkconfig: - 30 2

nginx-设置默认虚拟主机、设置域名重定向、设置用户认证

Nginx默认虚拟主机 编辑nginx.comf vim /usr/local/nginx/conf/nginx.conf 删除server段 加入include vhost/*.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_nofi

Nginx 配置默认虚拟主机

为什么要配置默认的虚拟主机:http://www.cnblogs.com/pzk7788/p/7039496.html [[email protected] ~]# vim /usr/local/nginx/conf/vhosts/default.conf server { listen 80 default_server; server_name localhost; index index.html index.htm index.php; root /tmp/1233; deny all;

46次课(Nginx安装 、 默认虚拟主机、Nginx用户认证、Nginx域名重定向)

Nginx安装 进入/usr/local/src目录下 [[email protected] ~]# cd /usr/local/src/ 下载Nginx安装包可以去nginx.org或者https://coding.net/u/aminglinux/p/resource/git/blob/master/README.md下载 [[email protected] src]# wget http://124.205.69.170/files/51490000069A64B9/nginx.org/

LAMP架构(nginx安装,默认虚拟主机,用户认证,域名重定向,nginx配置文件详解)

一.安装nginx [[email protected] conf]# wget http://nginx.org/download/nginx-1.8.0.tar.gz [[email protected] conf]# tar zxvf nginx-1.8.0.tar.gz [[email protected] conf]# cd nginx-1.8.0 [[email protected] conf]# ./configure --prefix=/usr/local/nginx [[ema

Nginx安装与配置:默认虚拟主机、用户认证和域名重定向

一.Nginx安装 1.下载并解压安装包 [[email protected] ~]# cd /usr/local/src/ [[email protected] src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz [[email protected] src]# tar zxf nginx-1.12.2.tar.gz 2.配置编译选项 [[email protected] src]# cd nginx-1.12.2 [[email 

12.6 Nginx安装;12.7 Nginx默认虚拟主机;12.8 Nginx用户认证;12.9

扩展: nginx.conf 配置详解 : http://www.ha97.com/5194.html http://my.oschina.net/duxuefeng/blog/34880 nginx rewrite四种flag : http://www.netingcn.com/nginx-rewrite-flag.html http://unixman.blog.51cto.com/10163040/1711943 12.6 Nginx安装 1. 进入下载安装包目录: [[email pro

2018-3-13 12周2次课 Nginx安装、默认虚拟主机、用户认证、域名重定向

12.6 Nginx安装 [[email protected] ~]# cd /usr/local/src/ [[email protected] src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz (过程省略) [[email protected] src]# tar zxvf nginx-1.12.2.tar.gz [[email protected] src]# cd nginx-1.12.2/ [[email protecte

12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx

12.6 Nginx安装 [[email protected] conf]# chkconfig --add nginx[[email protected] conf]# chkconfig nginx on[[email protected] conf]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configur