nginx-设置默认虚拟主机设置域名重定向设置用户认证
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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域名重定向
Nginx安装 默认虚拟主机 Nginx用户认证 Nginx域名重定向
nginx安装默认虚拟主机nginx用户认证nginx域名重定向