linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍
生活随笔
收集整理的這篇文章主要介紹了
linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Nginx介紹
- Nginx官網(wǎng)(http://nginx.org),最新版1.13,最新穩(wěn)定版1.12
- Nginx應(yīng)用場(chǎng)景:web服務(wù)、反向代理、負(fù)載均衡
- Nginx著名分支,淘寶基于Nginx開發(fā)的Tengine,使用上和Nginx一致,服務(wù)名,配置文件名都一樣,和Nginx的最大區(qū)別在于Tenging增加了一些定制化模塊,在安全限速方面表現(xiàn)突出,另外它支持對(duì)js,css合并
- Nginx核心+lua相關(guān)的組件和模塊組成了一個(gè)支持lua的高性能web容器openresty,參考 http://jinnianshilongnian.iteye.com/blog/2280928
Nginx安裝
1. 進(jìn)入存放源碼包目錄
[root@gary-tao ~]# cd /usr/local/src/ [root@gary-tao src]#2. 下載源碼包
[root@gary-tao src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz3. 解壓壓縮包
[root@gary-tao src]# tar zxf nginx-1.12.1.tar.gz4. 安裝nginx
[root@gary-tao src]# cd nginx-1.12.1 [root@gary-tao nginx-1.12.1]# ./configure --prefix=/usr/local/nginx5. 編譯nginx
[root@gary-tao nginx-1.12.1]# make [root@gary-tao nginx-1.12.1]# make install [root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/ //查看nginx核心配置文件 conf html logs sbin [root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/conf/ fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default [root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/html/ 50x.html index.html [root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/logs/ [root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/sbin/ nginx [root@gary-tao nginx-1.12.1]# ls /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx [root@gary-tao nginx-1.12.1]# /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 successful6. 創(chuàng)建nginx配置文件及編輯啟動(dòng)腳本
[root@gary-tao nginx-1.12.1]# vim /etc/init.d/nginx配置如下內(nèi)容:#!/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_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL } stop() {echo -n $"Stopping $prog: "killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL } reload() {echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL } restart() {stopstart } configtest() {$NGINX_SBIN -c $NGINX_CONF -treturn 0 } case "$1" instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1 esac exit $RETVAL7.修改啟動(dòng)腳本權(quán)限
[root@gary-tao nginx-1.12.1]# chmod 755 /etc/init.d/nginx8. 添加nginx服務(wù)
[root@gary-tao nginx-1.12.1]# chkconfig --add nginx9. 設(shè)置開機(jī)啟動(dòng)
[root@gary-tao nginx-1.12.1]# chkconfig nginx on10. 配置nginx的配置文件
[root@gary-tao nginx-1.12.1]# cd /usr/local/nginx/conf/ [root@gary-tao conf]# ls fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default [root@gary-tao conf]# mv nginx.conf nginx.conf.bak [root@gary-tao conf]# vim nginx.conf配置如下內(nèi)容: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;} } }11. 測(cè)試配置文件語(yǔ)法
[root@gary-tao 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 successful12. 開啟Nginx
[root@gary-tao conf]# /etc/init.d/nginx start Starting nginx (via systemctl): Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.[ 確定 ] [root@gary-tao conf]# systemctl daemon-reload //解決上面啟動(dòng)時(shí)報(bào)錯(cuò) [root@gary-tao conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 確定 ]13. 查詢nginx啟動(dòng)界面
[root@gary-tao conf]# ps aux |grep nginx root 62748 0.0 0.0 20500 624 ? Ss 18:51 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 62749 0.0 0.3 22944 3212 ? S 18:51 0:00 nginx: worker process nobody 62750 0.0 0.3 22944 3212 ? S 18:51 0:00 nginx: worker process root 62835 0.0 0.0 112680 976 pts/1 S+ 19:02 0:00 grep --color=auto nginx [root@gary-tao conf]# curl localhost //測(cè)試下頁(yè)面 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;} </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p><p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p> </body> </html> [root@gary-tao conf]# ls /usr/local/nginx/html/ //頁(yè)面文件目錄 50x.html index.html14.測(cè)試Nginx解析php
[root@gary-tao conf]# vim /usr/local/nginx/html/1.php 配置如下內(nèi)容:<?php echo "This is nginx test page."; ?>使用curl測(cè)試
root@gary-tao conf]# curl localhost/1.php This is nginx test page.[root@gary-tao conf]# [root@gary-tao conf]#Nginx默認(rèn)虛擬主機(jī)
在Nginx中也有默認(rèn)虛擬主機(jī),跟httpd類似,第一個(gè)被Nginx加載的虛擬主機(jī)就是默認(rèn)主機(jī),但和httpd不相同的地方是,它還有一個(gè)配置用來標(biāo)記默認(rèn)虛擬主機(jī),也就是說,如果沒有這個(gè)標(biāo)記,第一個(gè)虛擬主機(jī)為默認(rèn)虛擬主機(jī)。
1. 編輯配置文件:
vim /usr/local/nginx/conf/nginx.conf 增加以下內(nèi)容:include vhost/*.conf;如圖:
2. 創(chuàng)建一個(gè)vhost目錄
[root@gary-tao conf]# mkdir /usr/local/nginx/conf/vhost [root@gary-tao conf]# cd /usr/local/nginx/conf/vhost/ [root@gary-tao vhost]# vim aaa.conf //進(jìn)入vhost目錄下并創(chuàng)建編輯一個(gè).conf文件增加如下內(nèi)容:server {listen 80 default_server; // 有這個(gè)標(biāo)記的就是默認(rèn)虛擬主機(jī)server_name aaa.com;index index.html index.htm index.php;root /data/wwwroot/default; }3.創(chuàng)建default目錄
[root@gary-tao vhost]# mkdir /data/wwwroot/default [root@gary-tao vhost]# cd /data/wwwroot/default/ [root@gary-tao default]# ls [root@gary-tao default]# vim index.html在default目錄下的index.html文件中定義如下內(nèi)容:This is the default site.4.測(cè)試語(yǔ)法,重新加載配置文件(不需要重啟服務(wù))
[root@gary-tao default]# /usr/local/nginx/sbin/nginx -t //測(cè)試語(yǔ)法 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@gary-tao default]# /usr/local/nginx/sbin/nginx -s reload //重新加載配置5.使用curl測(cè)試
[root@gary-tao default]# curl localhost This is the default site. [root@gary-tao default]# curl -x127.0.0.1:80 123.com This is the default site. [root@gary-tao default]# curl -x127.0.0.1:80 aaaa.com This is the default site. [root@gary-tao default]# curl -x127.0.0.1:80 ddd.com This is the default site.解釋說明:
訪問的域名無論是指定的aaa.com還是其它域名,只要解析過來,指向到我們服務(wù)器,都能訪問到這個(gè)站點(diǎn),這就是默認(rèn)虛擬主機(jī)。
Nginx用戶認(rèn)證
1. 創(chuàng)建一個(gè)虛擬主機(jī)配置文件
[root@gary-tao conf]# vim /usr/local/nginx/conf/vhost/test.com.conf增加如下內(nèi)容: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; } }2.創(chuàng)建密碼文件
[root@gary-tao conf]# yum install -y httpd //如果之前沒有安裝過Apache的話就安裝httpd,是為了可以使用Apache的htpasswd工具創(chuàng)建用戶 [root@gary-tao conf]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd aming 備注:Apache自帶命令htpasswd創(chuàng)建密碼文件,-c是創(chuàng)建,-m是指定md5加密類型,指定用戶為aming(備注:如果再次新增用戶,就不需要再加 -c ,因?yàn)橐呀?jīng)創(chuàng)建過密碼文件了,加了會(huì)覆蓋之前的創(chuàng)建好的用戶) New password: Re-type new password: Adding password for user aming [root@gary-tao src]# cat /usr/local/nginx/conf/htpasswd aming:$apr1$o3zygnex$U.fWNEyk7.OeiwK.hcsBw/ [root@gary-tao src]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd xie New password: Re-type new password: Adding password for user xie [root@gary-tao src]# cat /usr/local/nginx/conf/htpasswd aming:$apr1$o3zygnex$U.fWNEyk7.OeiwK.hcsBw/ xie:$apr1$rBHXFLAp$fx9B2QPOtVQzLnz5hIWme13.測(cè)試語(yǔ)法及重新加載配置
[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload4.使用curl命令測(cè)試用戶認(rèn)證
[root@gary-tao src]# 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.1</center> </body> </html> [root@gary-tao src]# curl -x127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 13:01:05 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@gary-tao src]# curl -x127.0.0.1:80 -uaming:aming test.com -I //增加用戶訪問 HTTP/1.1 404 Not Found Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 13:01:36 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@gary-tao src]# mkdir /data/wwwroot/test.com //創(chuàng)建用戶目錄 [root@gary-tao src]# echo “test.com”>/data/wwwroot/test.com/index.html //在test.com目錄下編輯index.html [root@gary-tao src]# curl -x127.0.0.1:80 -uaming:aming test.com “test.com”針對(duì)目錄的用戶認(rèn)證
[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf //進(jìn)入配置文件,在location后面加上目錄名字就可以如圖:
使用curl測(cè)試:
[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload [root@gary-tao src]# curl -x127.0.0.1:80 test.com //訪問網(wǎng)站正常 “test.com” [root@gary-tao src]# curl -x127.0.0.1:80 test.com/admin/ //但是訪問admin目錄下時(shí)就會(huì)出現(xiàn)401,需要用戶認(rèn)證 <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@gary-tao src]# mkdir /data/wwwroot/test.com/admin //創(chuàng)建目錄地址 [root@gary-tao src]# echo "test.com adming dir" > /data/wwwroot/test.com/admin/index.html [root@gary-tao src]# curl -x127.0.0.1:80 test.com/admin/ <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@gary-tao src]# curl -x127.0.0.1:80 -uaming:aming test.com/admin/ //使用用戶密碼訪問正常 test.com adming dir針對(duì)文件的用戶認(rèn)證
[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf //進(jìn)入配置文件,在location后面加上匹配文件名字如圖:
使用curl測(cè)試,訪問admin目錄時(shí)是正常的,訪問admin.php就需要用戶認(rèn)證了
[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload [root@gary-tao src]# curl -x127.0.0.1:80 test.com/admin/ test.com adming dir [root@gary-tao src]# curl -x127.0.0.1:80 test.com/admin.php <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html>Nginx域名重定向
1. 更改test.com.conf
[root@gary-tao src]# vim /usr/local/nginx/conf/vhost/test.com.conf定義如下內(nèi)容:server {listen 80;server_name test.com test1.com test2.com; //server_name后面支持寫多個(gè)域名,這里要和httpd的做一個(gè)對(duì)比index index.html index.htm index.php;root /data/wwwroot/test.com;if ($host != 'test.com' ) {rewrite ^/(.*)$ http://test.com/$1 permanent; //permanent為永久重定向,狀態(tài)碼為301,如果寫redirect則為302} }如圖:
2. 測(cè)試語(yǔ)法及重新加載配置,使用curl測(cè)試
[root@gary-tao src]# /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@gary-tao src]# /usr/local/nginx/sbin/nginx -s reload [root@gary-tao src]# curl -x127.0.0.1:80 test2.com/index.html -I //訪問test2.com后會(huì)跳轉(zhuǎn)到test.com HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 13:30:03 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/index.html[root@gary-tao src]# curl -x127.0.0.1:80 test2.com/index.html/adgagadga -I //訪問test2.com后會(huì)跳轉(zhuǎn)到test.com HTTP/1.1 301 Moved Permanently Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 13:30:21 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/index.html/adgagadga[root@gary-tao src]# curl -x127.0.0.1:80 test3.com/index.html/adgagadga -I //訪問test3.com就跳轉(zhuǎn)到默認(rèn)虛擬主機(jī),報(bào)錯(cuò)404 HTTP/1.1 404 Not Found Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 13:30:38 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive轉(zhuǎn)載于:https://blog.51cto.com/taoxie/2057175
總結(jié)
以上是生活随笔為你收集整理的linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 记一笔冒泡排序和对它的优化
- 下一篇: 刨根问底——Handler