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

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

时间: 2024-07-29 10:48:44

nginx-设置默认虚拟主机、设置域名重定向、设置用户认证的相关文章

centos上安装nginx服务器实现虚拟主机和域名重定向

Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日.其将源代码以类BSD许可证的形式发布,因它的稳定性.丰富的功能集.示例配置文件和低系统资源的消耗而闻名.2011年6月1日,nginx 1.0.4发布. Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP

apache设置默认虚拟主机

默认虚拟主机 为了防止其他不是自己的域名解析到自己的IP,我们可以通过更改虚拟主机文件件/usr/local/apache2/conf/extra/httpd-vhosts.con 去限制其他域名.我们创建的新的虚拟主机,创建一个新的空目录将其权限设置为600.这样只有我们在配置文件中定义的域名在能访问. 命令如下: vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 在第一个虚拟主机的位置加入如下代码: <VirtualHost *:80>

Nginx 配置默认虚拟主机

为什么要配置默认的虚拟主机:http://www.cnblogs.com/pzk7788/p/7039496.html [[email protected] ~]# vim /usr/local/nginx/conf/vhosts/default.conf server { listen 80 default_server; server_name localhost; index index.html index.htm index.php; root /tmp/1233; deny all;

46次课(Nginx安装 、 默认虚拟主机、Nginx用户认证、Nginx域名重定向)

Nginx安装 进入/usr/local/src目录下 [[email protected] ~]# cd /usr/local/src/ 下载Nginx安装包可以去nginx.org或者https://coding.net/u/aminglinux/p/resource/git/blob/master/README.md下载 [[email protected] src]# wget http://124.205.69.170/files/51490000069A64B9/nginx.org/

nginx安装,虚拟主机,用户认证及域名重定向

nginx安装 cd /usr/local/src/ wget http://nginx.org/download/nginx-1.14.0.tar.gz tar zxfv nginx-1.14.0.tar.gz cd nginx-1.14.0/ ./configure --prefix=/usr/local/nginx make && make install 启动文件配置vim /etc/init.d/nginx,参考下面 #!/bin/bash # chkconfig: - 30 2

nginx虚拟主机和域名跳转

nginx介绍 nginx官网 :nginx.orgnginx主要应用web服务.反向代理和负载均衡的作用上nginx分支,淘宝基于nginx开发的Tengine,使用上和nginx一致,服务和配置名一致nginx比起apache在处理静态页面时更有优势,nginx最大区别在于Tenging支持一些定制化模块,在安全限速方面比较突出,支持js.css合并,优化web的高并发的访问需求nginx核心+lua相关组件和模块可以组成一个支持lua的高性能web容器openresty,openresty

httpd之虚拟主机和默认虚拟主机

原理介绍 基于IP地址的虚拟主机 不同的主机名解析到不同的IP地址,提供虚拟主机服务的机器上同时设置有这些IP地址.服务器根据用户请求的目的IP地址来判定用户请求的是哪个虚拟主机的服务,从而进一步的处理. 缺点:既浪费了IP地址,又限制了一台机器所能容纳的虚拟主机数目.因此这种方式越来越少使用.但是,这种方式是早期使用的HTTP 1.0协议唯一支持的虚拟主机方式. 基于主机名的虚拟主机 HTTP 1.1协议中增加了对基于主机名的虚拟主机的支持.具体说,当客户程序向WWW服务器发出请求时,客户想要

LAMP架构(nginx安装,默认虚拟主机,用户认证,域名重定向,nginx配置文件详解)

一.安装nginx [[email protected] conf]# wget http://nginx.org/download/nginx-1.8.0.tar.gz [[email protected] conf]# tar zxvf nginx-1.8.0.tar.gz [[email protected] conf]# cd nginx-1.8.0 [[email protected] conf]# ./configure --prefix=/usr/local/nginx [[ema

LNMP(2)Nginx默认虚拟主机、Nginx用户认证、Nginx域名重定向、Nginx访问日志、

Nginx默认虚拟主机 Nginx和httpd都有虚拟主机,在httpd中第一个被加载的就是默认虚拟主机:但是在Nginx中它有一个配置用来标记默认虚拟主机(default_server),如果不做标记,那么第一个也是默认为虚拟主机. 默认虚拟主机设置: 1.需改配置文件/usr/local/nginx/conf/nginx.conf cd /usr/local/nginx/conf/ vim nginx.conf 删除内容后,加上一行(在httpd{}里加)include vhost/*.co