Nginx安装,Nginx默认虚拟主机,Nginx用户认证,Nginx重定向

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chen112826473/article/details/79956679

Nginx安装

进入/usr/local/src目录下,下载一个Nginx的稳定版。

[root@shuai-01 ~]# cd /usr/local/src
[root@shuai-01 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

解压压缩包:

[root@shuai-01 src]# tar zxvf nginx-1.12.2.tar.gz

编译: 
进入Nginx目录下 
编译根据自己的要求来编译,选择一些模块。 
我这里只指定文件路径:

[root@shuai-01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx

接着make:

[root@shuai-01 nginx-1.12.2]# make

make install:

[root@shuai-01 nginx-1.12.2]# make install

[root@shuai-01 nginx-1.12.2]# ls /usr/local/nginx/
conf  html  logs  sbin

make时候出错:

make 
make: * No rule to make target build', needed bydefault’. Stop.

安装gcc

yum install gcc

解决办法是需要安装openssl以及ncurses组件

yum install -y openssl*

yum -y install ncurses-devel

conf : 配置文件目录 
html : 网页样例 
logs: 日志 
sbin: 进程,核心文件

给配置文件做一个启动脚本(/etc/init.d/nginx):

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
  • 1
  • 2

更改配置文件权限:

[root@shuai-01 nginx-1.12.2]# chmod 755 /etc/init.d/nginx 

添加为系统服务,并设置为开机自启动:

[root@shuai-01 nginx-1.12.2]# chkconfig --add nginx
[root@shuai-01 nginx-1.12.2]# chkconfig nginx on

配置Nginx的配置文件: 
Nginx的conf目录下有nginx.conf这个文件了。

[root@shuai-01 nginx-1.12.2]# cd /usr/local/nginx/conf
[root@shuai-01 conf]# 
[root@shuai-01 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf

不用,用自己创建的配置文件。 
先将nginx.conf改名

[root@shuai-01 conf]# mv nginx.conf nginx.conf.1

自己创建

[root@shuai-01 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;
    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;
        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;
        }    
    }
}

编辑配置文件后,检查语法:

[root@shuai-01 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@shuai-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  确定  ]

user 用来定义启动nginx服务的是那个用户。 
worker_processes 2; //子进程有两个

error_log /usr/local/nginx/logs/nginx_error.log crit; //错误日志

worker_rlimit_nofile 51200;//nginx最多打开多少个文件

server 和Apache配置文件中的virtual hosts 一样,对应虚拟主机。

    server
    {
        listen 80; //监听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; //指定PHP-fpm的监听端口或者监听sock
                监听127.0.0.1:9000
                fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        } // 配置解析PHP的部分
    }

nginx配置详解: 
http://www.ha97.com/5194.html 
https://my.oschina.net/duxuefeng/blog/34880

nginx默认虚拟主机

设置Nginx默认虚拟主机,其实默认就设置了。在Nginx的配置文件中,server就是。一般的,你有几个网站就设置几个server。还有另一种设置方式,在配置文件中不要去设置server,直接重新写一个虚拟主机配置文件(vhost/*.conf).

编辑配置文件,把Nginx配置文件中server段删去,添加一段:

include vhost/*.conf;

这里写图片描述

在/usr/local/nginx/conf/目录下,创建一个目录(vhost),并在目录下创建一个新文件。这个vhost就类似于Apache中的虚拟配置文件。

[root@shuai-01 conf]# mkdir vhost
[root@shuai-01 conf]# cd vhost/
[root@shuai-01 vhost]# ls
[root@shuai-01 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;
}

创建/data/wwwroot/default,并在defualt目录下编写index.html文件:

[root@shuai-01 vhost]# mkdir /data/wwwroot/default
[root@shuai-01 vhost]# cd /data/wwwroot/default/
[root@shuai-01 default]# vim index.html

echo “This is a default site.”>/data/wwwroot/default/index.html

检测一下配置文件语法

[root@shuai-01 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@shuai-01 default]# /usr/local/nginx/sbin/nginx -s reload

一般的,在服务器跑动的时候,都选择重新加载配置文件,而不是重启服务(/etc/init.d/nginx restart),重启服务会短暂关闭然后在启动。

测试默认主机:

[root@shuai-01 default]# curl localhost
this is a default site

默认虚拟主机就是只要你解析过来是这个IP,不管什么域名,都会访问到默认虚拟主机。

[root@shuai-01 default]# curl -x127.0.0.1:80 bbb.com
this is a default site
[root@shuai-01 default]# curl -x127.0.0.1:80 abadsj.com
this is a default site

Nginx用户认证

做用户认证就是为了安全,在做httpd的用户认证时就已经说到过,可以将两篇帖子结合起来看。 
http://blog.csdn.net/aoli_shuai/article/details/78854280

创建一个虚拟主机(test.com.conf):

[root@shuai-01 wwwroot]# cd /usr/local/nginx/conf/vhost/
[root@shuai-01 vhost]# ls
aaa.com.conf
[root@shuai-01 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;//用户名密码文件
     }
}

生成密码的工具是htpasswd,这个工具在Apache用户认证时就安装过了,没安装的就用yum install -y httpd 安装上。

为shuai用户做用户认证:

[root@shuai-01 vhost]# /usr/local/apache2.4/bin/htpasswd  -c /usr/local/nginx/conf/htpasswd shuai
New password: 
Re-type new password: 
Adding password for user shuai

为aoli用户做认证:

[root@shuai-01 vhost]# /usr/local/apache2.4/bin/htpasswd  /usr/local/nginx/conf/htpasswd aoli
New password: 
Re-type new password: 
Adding password for user aoli
[root@shuai-01 vhost]# cat /usr/local/nginx/conf/htpasswd 
shuai:$apr1$V0AiFAbc$CSttuCLed5mA1AMi1mKdw/
aoli:$apr1$OX2YLAuw$z1XF5XGLO/Z15Qw5dFo0V0

检查配置文件语法并重新加载配置文件:

[root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

测试:

[root@shuai-01 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>

出现401,需要用户认证。

[root@shuai-01 vhost]# curl -ushuai:123456 -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>

出现404,没有文件。 
写一个index.html文件。

[root@shuai-01 vhost]# mkdir /data/wwwroot/test.com
[root@shuai-01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@shuai-01 vhost]# curl -ushuai:123456 -x127.0.0.1:80 test.com
test.com

这个用户认证时针对整个站点,只针对某个特定目录的用户认证。针对admin目录。

修改虚拟配置文件:

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@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

测试:

访问test.com

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test.com
test.com

访问test.com/admin

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test.com/admin
<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>

针对某个.php文件

配置文件写成

location ~ admin.php

nginx域名重定向

更改虚拟配置文件

[root@shuai-01 vhost]# vim test.com.conf

server
{
    listen 80;
    server_name test.com test1.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  /admin/           
     {  
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
     }
}

这里写图片描述

这里多个域名都可以写到server_name 后面,不像httpd,有server_alias .

检查配置文件语法并重新加载配置文件:

    [root@shuai-01 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@shuai-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

测试:

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:30 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test3.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:42 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@shuai-01 vhost]# curl -x127.0.0.1:80 test4.com/index.html -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 08 Jan 2018 11:16:49 GMT
Content-Type: text/html
Content-Length: 23
Last-Modified: Mon, 08 Jan 2018 07:52:14 GMT
Connection: keep-alive
ETag: "5a53232e-17"
Accept-Ranges: bytes

测试test2.com test3.com 都是301重定向,test4.com 时,访问就是默认虚拟主机。

nginx rewrite四种flag http://www.netingcn.com/nginx-rewrite-flag.html


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页