12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向

时间:2021-01-06 14:27:23
12.6 Nginx安装
12.7 默认虚拟主机
12.8 Nginx用户认证

12.9 Nginx域名重定向


 Nginx安装

 
  1. wget http://nginx.org/download/nginx-1.8.0.tar.gz
  2. ./configure --prefix=/usr/local/nginx
  3. make && make install
  4. echo $?
  5. > /usr/local/nginx/conf/nfinx.conf //清空配置文件写入下面配置:
            
 
 
  
 
user nobody nobody; //启动服务用户
worker_processes 2; //子进程数量
error_log /usr/local /nginx/logs/nginx_error.log crit; //错误日志路径
pid /usr/local /nginx/logs/nginx.pid; //pid
worker_rlimit_nofile 51200; //打开文件最大量
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对应一个虚拟主机
{
listen 80; //设置端口
server_name localhost; //定义名字
index index.html index.htm index.php;
root /usr/local /nginx/html;
location ~ \.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;
}
}
}

6.vim /etc/init.d/nginx //编写启动脚本写入以下配置 并把权限改为755 设置开机启动:chkconfig --add nginx chkconfig /on

  •  
      
    #!/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

7. /usr/local/nginx/sbin/nginx -t //检查语法(提示success就没问题)

8./etc/init.d/nginx start  //开启服务

9. ps aux |grep nginx //检查服务

10.测试能否解析php:

        curl /usr/local/nginx/html/1.php

12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向



默认虚拟主机 12.8 

1.修改主机配置文件:添加以下一行

include vhost/*.conf   //意思是/usr/local/nginx/conf/vhost/下面所以以.conf结尾的文件都会被加载

2.在conf目录下创建vhost目录并写一个.conf配置文件

mkdir vhost 

vim /vhost/default.conf

3.在default.conf配置文件写入以下内容

server
{
listen 80 default_server;   //default标记默认虚拟主机
server_name aaa.com;      //域名
index index.html index.htm index.php;   //支持的解析
root /data/nginx;    //访问那个页面

}

4.检查语法

    /usr/local/nginx/sbin/nginx -t

5.创建data和nginx目录

mkdir data 

mkdir nginx

6.在nginx目录写入一个html

vim index.html 

            this is a test!

7.重新加载配置文件

    /usr/local/nginx/sbin/nginx -s reload

8。测试

curl localhost 

12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向


Nginx用户认证

1.创建多一个虚拟主机配置

    vim /usr/local/nginx/conf/vhost/test.conf

添加如下配置:

  •             server
    {
    listen 80;
    server_name test.com;   
    index index.html index.html index.php;
    root /data/nginx1;

    location /                             //指定要做用户认证目录、文件、html
    {
    auth_basic  "Auth";    //打开认证
    auth_basic_user_file /usr/local/nginx/conf/vhost/htpasswd;   //指定密码文件
    }
    }
  • /usr/local/nginx/sbin/nginx -t 检查语法 
  • yum install -y httpd   //安装httpd,因为要借用httpd生成密码文件
  • find  /  -name htpasswd  //查找htpasswd工具在哪
  • /usr/bin/htpasswd   //一般yum安装得再这里
  • /usr/bin/htpasswd -c /usr/local/nginx/conf/vhost/htpasswd test  // 生成密码文件,输入密码
  • mkdir nginx1  创建站点目录
  • cd  nginx1                                    echo “this is  test”> index.html  //写入内容
  • /usr/local/nginx/sbin/nginx -s reload  //重新加载配置文件
  • curl -x127.0.0.1:80 test.com  -utest:test    //测试   -u 输入账号密码
  • 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向

Nginx域名重定向

类似httpd的域名跳转

在原先用户认证上添加配置:如下

  • server
    {
    listen 80;
    server_name test.com test1.com test2.com ;
    index index.html index.html index.php;
    root /data/nginx1;


    if ($host != 'test.com' ){      //如果host不等于则rewrite
    rewrite ^/(.*)$ http://test.com/$1 permanent;   // ^任意开头(比如httpd://)  (.*)通配  $到结尾
    }

    }
  • permanent 表示永久重定向 (301)  redirect临时重定向(302)
  • /usr/local/nginx/sbin/nginx  -t  
  • /usr/local/nginx/sbin/nginx  -s reload
  • curl -I -x127.0.0.1:80 test1.com  //测试下图可以看到location跳转到 test.com
  • 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向