码迷,mamicode.com
首页 > 其他好文 > 详细

nginx-设置默认虚拟主机、设置域名重定向、设置用户认证

时间:2018-03-13 22:48:44      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:nginx域名重定向   nginx用户认证   nginx默认虚拟主机   

Nginx默认虚拟主机

编辑nginx.comf

vim /usr/local/nginx/conf/nginx.conf
删除server段
加入include vhost/*.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;
    include vhost/*.conf;
}

新建vhost目录

mkdir /usr/local/nginx/conf/vhost
cd /usr/local/nginx/conf/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;
}

创建default网站目录

mkdir /data/wwwroot/default
vim index.html
代码
This is the default test.

检错与重启测试

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

curl -x127.0.0.1:80 aaa.com
curl -x127.0.0.1:80 bbb.com

nginx用户认证

编辑web配置文件

cd /usr/local/nginx/conf/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;
}
}

生成密码文件

/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd admin
注意:如果没有安装apache那么就需要yum install -y httpd
然后htpasswd -c /usr/local/nginx/conf/htpasswd admin

创建test.com目录

mkdir /data/wwwroot/test.com
vim 1.html
输入网页代码This is test.com/1.html

测错与应用配置

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

测试

curl -uadmin:admin -x127.0.0.1:80 test.com/1.html
This is test.com/1.html

只针对目录用户认证

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

修改 location / 为location /admin/
也就是将代表所有的/改为代表目录的/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;
}
}

针对单个页面

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

修改 location / 为location ~ admin.php
也就是将代表所有的/改为代表目录的/admin/


域名重定向

编辑web配置文件

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

增加

 if ($host != ‘test.com‘ ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }

修改server_name 后面增加test1.com

代码预览

server
{
    listen 80;
    server_name test.com test1.com test2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != ‘test.com‘ ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
}

检错与重新加载

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

测试
访问test1.com定位到test.com上了,成功

curl -x127.0.0.1:80 test1.com/1.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Tue, 13 Mar 2018 13:41:57 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/1.html

nginx-设置默认虚拟主机、设置域名重定向、设置用户认证

标签:nginx域名重定向   nginx用户认证   nginx默认虚拟主机   

原文地址:http://blog.51cto.com/shuzonglu/2086249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!