nginx-設置默認虛擬主機、設置域名重定向、設置用戶認證
阿新 • • 發佈:2018-03-14
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-設置默認虛擬主機、設置域名重定向、設置用戶認證