当前位置: 首页 > news >正文

Nginx安装、默认虚拟主机、用户认证、域名重定向

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]# vim /usr/local/nginx/conf/nginx.conf
......
将server部分去掉。
添加下面这行:
include vhost/*.conf;
#创建一个虚拟主机配置文件子目录(相当于增加子虚拟主机)

创建vhost目录:
[root@localhost conf]# mkdir vhost

在vhost目录下创建一台虚拟主机:
[root@localhost vhost]# vim aaa.com.conf
server
{
listen 80 default_server; 
#有'default_server'标记的就是默认虚拟主机
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}

创建配置文件中root指定的目录
[root@localhost vhost]# mkdir -p /data/wwwroot/default

添加索引页:
[root@localhost vhost]# vim /data/wwwroot/default/index.html

This is the default directory.

检测配置文件:
[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
#也可以重启nginx服务。

检测:
[root@localhost vhost]# curl localhost
This is the default directory.
#添加一台虚拟主机,所谓默认虚拟主机就是/usr/local/nginx/conf/vhost目录下虚拟主机配置文件中有“default_server”标记的虚拟主机。

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]# vim test.com.conf 
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;
    # 主域名为test.com,输入其它域名会跳转到主域名。
}

检测并重载配置:
[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 -x127.0.0.1:80 test2.com/index.html -I
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
#访问test2.com、test3.com都会跳转到test.com。访问test.com会提示404.

转载于:https://blog.51cto.com/754599082/2057215

相关文章:

  • 七周七数据库
  • Android FM模块学习之四源码分析(八)
  • 初识MongoDB分片
  • BZOJ 2821 作诗(Poetize)(分块)
  • python学习笔记(九):操作数据库
  • Java今年最流行的三大框架你应该学习了
  • JSON数组,JSON对象,数组的区别与基本操作整理
  • 阿里云全球19个地域节点,哪个节点的服务器好,速度快?
  • 回顾2017:基础设施支出增长 思科占主导地位
  • 微服务入门【系列视频课程】
  • mongodb集群模式(主从模式,副本集模式,分片模式)
  • 透彻影像王书浩:三易其辙与功不唐捐
  • 如何使用 Spinnaker 和 Kubernetes 进行数据库变更发布
  • 为什么需要模版成员方法
  • W3C官方推荐使用新发布的HTML5.2
  • 【391天】每日项目总结系列128(2018.03.03)
  • 0基础学习移动端适配
  • es6--symbol
  • JS基础之数据类型、对象、原型、原型链、继承
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • Magento 1.x 中文订单打印乱码
  • nginx 配置多 域名 + 多 https
  • Node + FFmpeg 实现Canvas动画导出视频
  • node学习系列之简单文件上传
  • PHP 使用 Swoole - TaskWorker 实现异步操作 Mysql
  • redis学习笔记(三):列表、集合、有序集合
  • windows下如何用phpstorm同步测试服务器
  • -- 查询加强-- 使用如何where子句进行筛选,% _ like的使用
  • 老板让我十分钟上手nx-admin
  • 在Docker Swarm上部署Apache Storm:第1部分
  • #laravel 通过手动安装依赖PHPExcel#
  • #pragma data_seg 共享数据区(转)
  • $.ajax,axios,fetch三种ajax请求的区别
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (LeetCode 49)Anagrams
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (zhuan) 一些RL的文献(及笔记)
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • ***测试-HTTP方法
  • *++p:p先自+,然后*p,最终为3 ++*p:先*p,即arr[0]=1,然后再++,最终为2 *p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1]
  • .Net Core webapi RestFul 统一接口数据返回格式
  • .net core 控制台应用程序读取配置文件app.config
  • .net 微服务 服务保护 自动重试 Polly
  • .NET版Word处理控件Aspose.words功能演示:在ASP.NET MVC中创建MS Word编辑器
  • [Android Pro] Notification的使用
  • [C#] 我的log4net使用手册
  • [C#]手把手教你打造Socket的TCP通讯连接(一)
  • [C++]模板与STL简介
  • [HTML]Web前端开发技术12(HTML5、CSS3、JavaScript )——喵喵画网页
  • [iOS]-NSTimer与循环引用的理解
  • [ISCTF 2023]——Web、Misc较全详细Writeup、Re、Crypto部分Writeup
  • [java基础揉碎]关系运算符(比较运算符)逻辑运算符赋值运算符三元运算符运算符的优先级
  • [java刷算法]牛客—剑指offer链表有环的入口、反转链表、合并排序链表