一、nginx安裝:javascript
(1):下載nginx安裝包;php
[root@localhost_001 src]# wget http://nginx.org/download/nginx-1.4.7.tar.gzcss
(2):解壓;html
[root@localhost_001 src]# tar -zxvf nginx-1.4.7.tar.gz java
(3):初始化nginx; ./configure --prefix=/usr/local/nginxnode
[root@localhost_001 nginx-1.4.7]# ./configure --prefix=/usr/local/nginxlinux
(4):make && make install;nginx
[root@localhost_001 nginx-1.4.7]# make && make installgit
(5):查看nginx的目錄;apache
[root@localhost_001 nginx-1.4.7]# ls /usr/local/nginx/
conf html logs sbin
conf:配置文件目錄;
html:樣例文件;
logs:存放日記;
sbin:核心進程目錄;(支持 -t 語法檢測錯誤)
(6):建立nginx的啓動腳本;也能夠這裏複製:nginx啓動腳本配置
[root@localhost_001 nginx-1.4.7]# vim /etc/init.d/nginx #!/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_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL [root@localhost_001 nginx-1.4.7]#
(7):更改啓動權限;
[root@localhost_001 nginx-1.4.7]# vim /etc/init.d/nginx
(8):nginx加入到服務列表,並開機啓動nginx;
[root@localhost_001 nginx-1.4.7]# chkconfig --add nginx [root@localhost_001 nginx-1.4.7]# chkconfig nginx on [root@localhost_001 nginx-1.4.7]# chkconfig --list nginx nginx 0:關 1:關 2:開 3:開 4:開 5:開 6:關
(9):建立nginx的配置文件; /usr/local/nginx/conf/nginx.conf(默認存在)
註釋:這裏不用它自帶的配置文件,咱們自定義便可; nginx配置文件:nginx配置文件
[root@localhost_001 conf]# vim nginx.conf user nobody nobody; #以那個用戶的身份來啓動nginx; worker_processes 2; #子進程有幾個; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; #nginx最多能夠打開多少個文件; events { use epoll; #使用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這幾行,那麼nginx將監聽不到80端口,致使服務不可用; server { listen 80; server_name localhost; 域名; index index.html index.htm index.php; root /usr/local/nginx/html; #根目錄 location ~ \.php$ { include fastcgi_params; #調用php; fastcgi_pass unix:/tmp/php-fcgi.sock; 指定監聽方式; 127.0.0.1:9000 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
(10):檢測是否存在錯誤; -t
[root@localhost_001 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 successful
(11):啓動nginx;
[root@localhost_001 conf]# service nginx start Starting nginx (via systemctl): [ 肯定 ]
詳細;安裝步驟以下;
[root@localhost_001 src]# wget http://nginx.org/download/nginx-1.4.7.tar.gz --2018-10-16 10:17:18-- http://nginx.org/download/nginx-1.4.7.tar.gz 100%[===========================================================================================>] 2018-10-16 10:17:24 (148 KB/s) - 已保存 「nginx-1.4.7.tar.gz」 [769153/769153]) [root@localhost_001 src]# tar -zxvf nginx-1.4.7.tar.gz #解壓 #生成編譯所須要的文件; [root@localhost_001 src]# cd nginx-1.4.7 [root@localhost_001 nginx-1.4.7]# ./configure --prefix=/usr/local/nginx [root@localhost_001 nginx-1.4.7]# make && make install #建立nginx啓動腳本: [root@localhost_001 nginx-1.4.7]# vim /etc/init.d/nginx [root@localhost_001 nginx-1.4.7]# vim /etc/init.d/nginx #更改啓動權限; [root@localhost_001 nginx-1.4.7]# chkconfig --add nginx [root@localhost_001 nginx-1.4.7]# chkconfig nginx on [root@localhost_001 nginx-1.4.7]# chkconfig --list nginx nginx 0:關 1:關 2:開 3:開 4:開 5:開 6:關 更改nginx的配置文件內容: [root@localhost_001 conf]# vim nginx.conf [root@localhost_001 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 successful [root@localhost_001 conf]# service nginx start Starting nginx (via systemctl): [ 肯定 ]
2:查看nginx相關進程;
[root@localhost_001 conf]# ps aux |grep nginx root 3429 0.0 0.0 24844 784 ? Ss 10:59 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 3430 0.0 0.1 27148 3356 ? S 10:59 0:00 nginx: worker process nobody 3431 0.0 0.1 27148 3356 ? S 10:59 0:00 nginx: worker process root 3434 0.0 0.0 112720 968 pts/0 R+ 11:00 0:00 grep --color=auto nginx
三、測試nginx和php; 瀏覽器裏輸入:curl localhost
[root@localhost_001 ~]# curl localhost <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; <p><em>Thank you for using nginx.</em></p> </body> </html> [root@lo
用瀏覽器訪問:IP地址;
測試php;
hello nginx.[root@localhost_001 ~]# cat /usr/local/nginx/html/1.php <?php echo "hello nginx."; [root@localhost_001 ~]# curl localhost/1.php hello nginx.[root@localhost_001 ~]#
二、默認虛擬主機;
(1):首先須要刪除配置/usr/local/nginx/conf/nginx.conf裏:server的那一段;而後添加 include vhost/.conf
[root@localhost_001 conf]# cat nginx.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; #新增這一行,必須的;(刪除服務的那些內容) }
(2):建立虛擬主機目錄vhost,在其目錄下配置虛擬主機; /usr/local/nginx/conf/vhost/aaa.com.conf
[root@localhost_001 conf]# mkdir /usr/local/nginx/conf/vhost [root@localhost_001 conf]# cd vhost/ [root@localhost_001 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; }
(3):建立網站根目錄及默認頁;
[root@localhost_001 vhost]# mkdir -p /data/wwwroot/default [root@localhost_001 vhost]# cd /data/wwwroot/default/ [root@localhost_001 default]# vim index.html This is a default site.
(4):檢測並重啓nginx服務;
[root@localhost_001 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 successful [root@localhost_001 conf]# service nginx restart Restarting nginx (via systemctl): [ 肯定 ]
註釋:或者從新加載也能夠; /usr/local/nginx/sbin/nginx -s reload
測試默認頁;
[root@localhost_001 conf]# curl localhost This is a default site. [root@localhost_001 conf]# curl localhost:80 bbb.com This is a default site.
註釋:nginx支持include這種語法;
1.默認虛擬主機,是根據目錄的第一個.conf了進行選擇,因此只須要在vhost目錄下依次建立就能夠了,而後依次查找便可;固然這種方法不智能 2.只須要在vhost目錄的.conf配置文件內,加上一個「default_server 」便可,把當前的這個配置對應的網站設置爲第一個默認虛擬主機;
三、用戶驗證;
針對真個網站作驗證;
(1):再此新建一個虛擬主機test.com.conf //內容以下:
[root@localhost_001 vhost]# vim test.com.conf [root@localhost_001 vhost]# cat test.com.conf server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / #表示全站,都須要進行用戶認證 #location /admin #這個地方只要加上」 /admin 」 就變成 針對這個站點的「admin」 這個目錄須要用戶認證 #location ~ admin.php #若是把這行這樣寫,就會變成,匹配 「 admin.php 」這個頁面的時候才須要用戶認證 { auth_basic "Auth"; #定義用戶認證的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; #用戶名密碼文件 } }
建立網站相關配置文件;
[root@localhost_001 vhost]# mkdir /data/wwwroot/test.com [root@localhost_001 vhost]# echo "test.com fast" > /data/wwwroot/test.com/index.html
(2):驗證的話須要生成密碼文件,這時也用到了apache的密碼生成工具;
註釋:若本機已經安裝過httpd,則能夠直接使用下面httpasswd生成; -c後面跟生成的目錄位置及用戶名;
htpasswd -c /usr/local/nginx/conf/htpasswd fenye
安裝並使用htpasswd生成密碼文件;
[root@localhost_001 vhost]# /usr/bin/htpasswd -c /usr/local/nginx/conf/htpasswd fenye New password: Re-type new password: Adding password for user fenye [root@localhost_001 vhost]# cat /usr/local/nginx/conf/htpasswd fenye:$apr1$hg.HAMRG$A9SkSCe6vomTaxlmT9s7t0
註釋:關於htpasswd這個工具在第一次使用須要使用-c來建立這個文件,第二次使用的時候由於已經有htpasswd這個文件,則不須要使用-c來建立,如若強制使用,將覆蓋以前的文件;
[root@localhost_001 vhost]# /usr/bin/htpasswd /usr/local/nginx/conf/htpasswd user1 New password: Re-type new password: Adding password for user user1 [root@localhost_001 vhost]# cat /usr/local/nginx/conf/htpasswd fenye:$apr1$hg.HAMRG$A9SkSCe6vomTaxlmT9s7t0 user1:$apr1$yfKA2qd/$X9IatIA9XfweuEVvyUngU/
(3):檢測配置文件是否有錯誤,並從新加載配置文件;
[root@localhost_001 vhost]# /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@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
註釋:使用 -s reload 從新加載配置和 restart重啓服務的區別;
使用 -s reload 若配置文件存在錯誤,則配置文件生效;
使用 restart 重啓,若配置文件存在錯誤,則會影響到網站的運行;
(4):測試,用curl命令訪問: curl -x127.0.0.1:80 test.com
[root@localhost_001 vhost]# 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.4.7</center> </body> </html>
註釋:這時候提示狀態碼401,則須要驗證; 下面用curl命令來驗證;
此時使用建立的兩個用戶fenye和user1來測試訪問;
[root@localhost_001 vhost]# curl -ufenye:nihao123! -x127.0.0.1:80 test.com test.com fast [root@localhost_001 vhost]# curl -uuser1:nihao123@ -x127.0.0.1:80 test.com test.com fast
註釋:如上的驗證時針對真個網站來的;
那如何針對一個目錄作驗證了;
好比當訪問admin目錄時,才須要作驗證;
首先建立admin目錄,並新建訪問默認頁,以下;
[root@localhost_001 vhost]# mkdir /data/wwwroot/test.com/admin [root@localhost_001 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
(1):嘗試訪問下,能夠正常訪問;
[root@localhost_001 vhost]# curl -uuser1:nihao123 -x127.0.0.1:80 www.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.4.7</center> </body> </html>
(2):修改虛擬主機配置文件; /usr/local/nginx/conf/vhost/test.com.conf
[root@localhost_001 vhost]# cat test.com.conf server { listen 80; server_name www.test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location /admin ####這個地方只要加上」 /admin 」 就變成 針對這個站點的「admin」 這個目錄須要用戶認證 auth_basic "Auth"; #定義用戶認證的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; #用戶名密碼文件 } }
註釋:圖示中location /admin這一行則表示對目錄進行驗證;
(3):而後檢測配置文件,並從新加載配置文件;
[root@localhost_001 vhost]# /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@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(4):測試訪問;發現訪問www.test.com不須要驗證,而訪問它下面的/admin/目錄則須要驗證;顯示Z;
[root@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@localhost_001 vhost]# curl -x127.0.0.1:80 www.test.com test.com fast [root@localhost_001 vhost]# curl -x127.0.0.1:80 www.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.4.7</center> </body> </html>
(5):輸入用戶名和密碼測試訪問;
[root@localhost_001 vhost]# curl -ufenye:nihao123! -x127.0.0.1:80 www.test.com/admin/ test.com admin dir [root@localhost_001 vhost]# curl -uuser1:nihao123@ -x127.0.0.1:80 www.test.com/admin/ test.com admin dir
三、針對URL作驗證;
好比針對admin.php作驗證;
1:修改虛擬主機配置文件/usr/local/nginx/conf/vhost/test.com.conf定義,在location後面加上 ~admin.php便可;
[root@localhost_001 vhost]# cat test.com.conf server { listen 80; server_name www.test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location ~ admin.php #若是把這行這樣寫,就會變成,匹配 「 admin.php 」這個頁面的時候才須要用戶認證 { auth_basic "Auth"; #定義用戶認證的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; #用戶名密碼文件 } }
2:建立admin.php的配置文件;
[root@localhost_001 vhost]# echo "test admin.php" > /data/wwwroot/test.com/admin.php
(3):檢測配置文件是否錯誤而後從新加載配置文件;
[root@localhost_001 vhost]# /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@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(4):測試,使用curl命令來測試;
[root@localhost_001 vhost]# curl -x 127.0.0.1:80 www.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.4.7</center> </body> </html> 輸入密碼後驗證; [root@localhost_001 vhost]# curl -uuser1:nihao123@ -x127.0.0.1:80 www.test.com/admin.php test admin.php [root@localhost_001 vhost]# curl -ufenye:nihao123! -x127.0.0.1:80 www.test.com/admin.php test admin.php
四、nginx域名重定向;
在apache裏面也有重定向,只是apache的 server_name只能跟一個域名,須要跟多個域名,則須要加alias;
在nginx裏面 server_name須要設置多個域名,就會使網站的權重變了,那麼到底使用哪一個作爲主域名了,這時候須要重定向功能了;
(1):修改虛擬主機配置文件; /usr/local/nginx/conf/vhost/test.com.conf
[root@localhost_001 vhost]# cat test.com.conf server { listen 80; server_name www.test.com bbs.test.com test1.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'www.test.com' ) { rewrite ^/(.*)$ http://www.test.com/$1 permanent; } }
註釋: if ($host !='www.test.com') 表示加入域名不等於www.test.com,將執行下面的腳本;
註釋:permanent是301的意思;
註釋:redirect則表示302的意思;
(2):檢測配置文件是否有錯誤,並從新加載配置文件;
[root@localhost_001 vhost]# /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@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(3):測試,用curl命令來測試;
[root@localhost_001 vhost]# curl -x127.0.0.1:80 bbs.test.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.4.7 Date: Tue, 16 Oct 2018 07:54:50 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://www.test.com/index.html [root@localhost_001 vhost]# curl -x127.0.0.1:80 test1.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.4.7 Date: Tue, 16 Oct 2018 07:55:56 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://www.test.com/index.html
擴展 nginx.conf 配置詳解 http://www.ha97.com/5194.html http://my.oschina.net/duxuefeng/blog/34880 nginx rewrite四種flag http://www.netingcn.com/nginx-rewrite-flag.html http://unixman.blog.51cto.com/10163040/1711943