美文网首页nginx
Nginx安装、默认虚拟主机、Nginx用户认证、Nginx域名

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

作者: XiaoMing丶 | 来源:发表于2018-11-26 22:07 被阅读0次

目录

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

一、Nginx安装

  • 下载和解压Nginx
[root@minglinux-01 /usr/local/src] wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@minglinux-01 /usr/local/src] tar zxvf nginx-1.12.2.tar.gz 
  • 初始化配置编译选项
[root@minglinux-01 /usr/local/src] cd nginx-1.12.2/
[root@minglinux-01 /usr/local/src/nginx-1.12.2] ./configure --prefix=/usr/local/nginx    
//指定安装路径

  • 编译和安装Nginx
[root@minglinux-01 /usr/local/src/nginx-1.12.2] make && make install

[root@minglinux-01 /usr/local/src/nginx-1.12.2] ls /usr/local/nginx/
conf  html  logs  sbin    
//conf:配置文件目录
//html:样例文件目录
//logs:存放日志目录
//sbin:核心文件目录

[root@minglinux-01 /usr/local/src/nginx-1.12.2] /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@minglinux-01 /usr/local/src/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=$?
    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@minglinux-01 /usr/local/src/nginx-1.12.2] chmod 755 /etc/init.d/nginx  //更改权限为755
[root@minglinux-01 /usr/local/src/nginx-1.12.2] chkconfig --add nginx //加入系统服务
[root@minglinux-01 /usr/local/src/nginx-1.12.2] chkconfig  nginx on //开机启动Nginx
  • 更改Nginx的配置文件
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] mv nginx.conf nginx.conf.1  //不用原有的,新建一个
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] vim nginx.conf
写入以下内容

user nobody nobody;    //运行nginx的用户为nobody,
worker_processes 2;  //nginx的子进程数量
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  //每个server对应一个虚拟主机,就像apache中的每个virtualhost对应一个虚拟主机,server部分未配置则无法监听80端口
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;  //网站根目录

        location ~ \.php$  //解析php相关内容
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            #fastcgi_pass 127.0.0.1:9000 
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}
  • 检查配置文件然后启动nginx
[root@minglinux-01 /usr/local/src/nginx-1.12.2/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
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] /usr/local/apache2.4/bin/apachectl stop  //关闭httpd进程
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] /etc/init.d/nginx  start
Starting nginx (via systemctl):                            [  确定  ]
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] ps aux |grep nginx
root        430  0.0  0.0  20536   608 ?        Ss   22:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      431  0.0  0.0  20976  1056 ?        S    22:06   0:00 nginx: worker process
root        433  0.0  0.0 112720   984 pts/1    S+   22:07   0:00 grep --color=auto nginx

nginx通常父进程用户为root,子进程用户为nobody
php-fpm通常父进程用户为root,子进程用户为php-fpm

  • 测试是否正确解析PHP
[root@minglinux-01 /usr/local/src/nginx-1.12.2/conf] vim /usr/local/nginx/html/1.php

<?php
    echo "PHP解析成功\n";
?>
[root@minglinux-01 /usr/local/nginx/conf] curl localhost/1.php
PHP解析成功

二、默认虚拟主机

  • 修改主配置文件nginx.conf,/删除原先server的配置,在结束符号}上面加入一行配置,如下所示:
 include vhost/*.conf;  
}
  • 在conf目录下创建一个vhost子目录
[root@minglinux-01 /usr/local/nginx/conf] pwd
/usr/local/nginx/conf
[root@minglinux-01 /usr/local/nginx/conf] mkdir vhost

  • vhost目录下创建aaa.com.conf
    写入以下内容
server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}
[root@minglinux-01 /usr/local/nginx/conf] mkdir -p /data/wwwroot/default
//创建/data/wwwroot/default
  • 测试
[root@minglinux-01 /data/wwwroot/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@minglinux-01 /data/wwwroot/default] /usr/local/nginx/sbin/nginx -s reload  //重新加载
[root@minglinux-01 /data/wwwroot/default] vim /data/wwwroot/default/index.html
写入
Hello,thank you!

[root@minglinux-01 /data/wwwroot/default] curl localhost
Hello,thank you!
[root@minglinux-01 /data/wwwroot/default] curl -x127.0.0.1:80 aaa.com
Hello,thank you!
[root@minglinux-01 /data/wwwroot/default] curl -x127.0.0.1:80 a.com
Hello,thank you!  //aaa.com是默认虚拟主机

  1. vhost目录下虚拟主机配置文件中可以用default_server指定该虚拟主机为默认虚拟主机。
  2. 如果目录下有多个虚拟主机配置文件都没有使用default_server指定为默认,那排序靠前的.conf配置文件的虚拟主机为默认虚拟主机。

三、Nginx用户认证

  1. 整站做用户认证
[root@minglinux-01 ~] 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;
    }
}

[root@minglinux-01 ~] /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd ming    //生成密码文件,指定一个用户
New password: 
Re-type new password: 
Adding password for user ming
[root@minglinux-01 ~] cat /usr/local/nginx/conf/htpasswd 
ming:$apr1$RbHhD.B/$Cy5W5B1r2535.f5TYJTdL.
[root@minglinux-01 ~] /usr/local/apache2.4/bin/htpasswd  /usr/local/nginx/conf/htpasswd ming1
New password: 
Re-type new password: 
Adding password for user ming1
[root@minglinux-01 ~] cat /usr/local/nginx/conf/htpasswd 
ming:$apr1$RbHhD.B/$Cy5W5B1r2535.f5TYJTdL.
ming1:$apr1$TsZ0Z5jj$.4i0ecCyxoj59p6Hh.nUu.

再次使用htpasswd工具的-c选项生成密码会覆盖之前的账户密码,不加-c则不会覆盖

  • 测试
[root@minglinux-01 ~] /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@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload  //重新加载配置,不会重启Nginx

[root@minglinux-01 ~] mkdir /data/wwwroot/test.com
[root@minglinux-01 ~] echo "test page" >/data/wwwroot/test.com/index.html
[root@minglinux-01 ~] curl -uming:123456 -x127.0.0.1:80 test.com 
test page

  1. 部分目录做用户认证
[root@minglinux-01 ~] 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  /admin   //在这里改变目录即可
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}
  • 测试
[root@minglinux-01 ~] /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@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 ~] curl -x127.0.0.1:80 test.com 
test page
[root@minglinux-01 ~] curl -x127.0.0.1:80 test.com/admin/ -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 13:27:07 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

[root@minglinux-01 ~] curl -uming:123456 -x127.0.0.1:80 test.com/admin/ -I
HTTP/1.1 404 Not Found                    //加上用户名密码后可以访问
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 13:28:49 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

  1. 单独url做用户认证
[root@minglinux-01 ~] 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  ~admin.php   //匹配admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}

  • 测试
[root@minglinux-01 ~] /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@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 ~] curl -x127.0.0.1:80 test.com/admin.php -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 13:46:09 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

[root@minglinux-01 ~] curl -uming:123456 -x127.0.0.1:80 test.com/admin.php
admin page

四、Nginx域名重定向

  • 修改配置文件
[root@minglinux-01 ~] vim /usr/local/nginx/conf/vhost/test.com.conf


{
    listen 80;
    server_name test.com test2.com test3.com;  //server_name后面可以跟多个域名
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }   
         //这里的^/(.*)$写全则为http://$host/(.*)$,permanent为永久重定向,相当于httpd的R=301。另外还有一个常用的redirect,相当于httpd的R=302。
    location  ~admin.php
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }   
}
  • 测试
[root@minglinux-01 ~] /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@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 ~] curl -x127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 14:02:45 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/

[root@minglinux-01 ~] curl -x127.0.0.1:80 test2.com/abcd -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 26 Nov 2018 14:03:40 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/abcd

扩展

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

相关文章

网友评论

    本文标题:Nginx安装、默认虚拟主机、Nginx用户认证、Nginx域名

    本文链接:https://www.haomeiwen.com/subject/cqvpqqtx.html