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.


本文转自 豆渣锅 51CTO博客,原文链接:
http://blog.51cto.com/754599082/2057215


相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
19天前
|
负载均衡 Java 应用服务中间件
nginx安装在linux上
nginx安装在linux上
43 2
|
24天前
|
应用服务中间件 nginx
树莓派安装Nginx服务结合内网穿透实现无公网IP远程访问
树莓派安装Nginx服务结合内网穿透实现无公网IP远程访问
|
4天前
|
应用服务中间件 nginx Docker
docker安装nginx
`docker search`找镜像,`pull`下载,后台 `-d` 运行容器,命名 `--name`,映射端口 `-p`。本机测试,确保服务器安全组开放端口,公网通过`http://ip:port`访问。用`docker stop id`停止容器。[查看详情](https://blog.csdn.net/javayoungcoolboy/article/details/134976510)
|
5天前
|
网络协议 应用服务中间件 nginx
nginx 302 301 设置 url 转跳 nginx 资源重定向 nginx tcp 和 http 转发
nginx 代理后端网站,和 网站资源目录重定向到其他连接地址
40 3
|
5天前
|
应用服务中间件 网络安全 nginx
nginx(1.13.7)首次安装出现:【make: *** 没有规则可以创建“default”需要的目标“build” 问题】解决措施
nginx(1.13.7)首次安装出现:【make: *** 没有规则可以创建“default”需要的目标“build” 问题】解决措施
|
8天前
|
Ubuntu 应用服务中间件 nginx
ubuntu编译安装nginx及安装nginx_upstream_check_module模块
以上是编译安装Nginx和安装 `nginx_upstream_check_module`模块的基本步骤。根据你的需求和环境,你可能需要进一步配置Nginx以满足特定的要求。
19 3
|
13天前
|
弹性计算 应用服务中间件 Shell
一键编译安装Nginx脚本
【4月更文挑战第30天】
22 1
|
14天前
|
关系型数据库 MySQL 应用服务中间件
centos7在线安装jdk1.8+tomcat+mysql8+nginx+docker
现在,你已经成功在CentOS 7上安装了JDK 1.8、Tomcat、MySQL 8、Nginx和Docker。你可以根据需要配置和使用这些服务。请注意,安装和配置这些服务的详细设置取决于你的具体需求。
53 2
|
18天前
|
负载均衡 前端开发 应用服务中间件
Nginx安装与使用
Nginx安装与使用
48 0
|
20天前
|
应用服务中间件 Linux 网络安全
【Linux】中如何安装nginx
【Linux】中如何安装nginx
38 0