可去官網(http://nginx.org)下載至Windows,用rz命令上傳php
[root@linux-10 ~]# cd /usr/local/src [root@linux-10 src]# rz
[root@linux-10 src]# tar -zxvf nginx-1.14.0.tar.gz
[root@linux-10 src]# cd nginx-1.14.0 [root@linux-10 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
注:初始化時最好根據本身的需求增長相關模塊的編譯參數,這裏因爲課程須要暫不加參數。html
make && make install
一樣支持-t選項linux
[root@linux-10 nginx-1.14.0]# ls /usr/local/nginx/sbin/ nginx [root@linux-10 nginx-1.14.0]# /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
vim /etc/init.d/nginx //複製以下內容 (參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )
chmod 755 /etc/init.d/nginx
chkconfig --add nginx chkconfig nginx on
將原配置文件重命名,建立一個新的配置文件nginx
[root@linux-10 nginx-1.14.0]# cd /usr/local/nginx/conf [root@linux-10 conf]# mv nginx.conf nginx.conf.bak
vim nginx.conf //寫入以下內容 (參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/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; //定義pid worker_rlimit_nofile 51200; //定義最大能夠打開的文件數量 events { use epoll; worker_connections 6000; //定義最大鏈接數 }
注:上述僅是部分代碼git
fastcgi_pass unix:/tmp/php-fcgi.sock;
server配置中的監聽端口要與PHP中的配置文件保持一致vim
[root@linux-10 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@linux-10 conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 肯定 ]
[root@linux-10 conf]# netstat -lntp |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4066/nginx: master
在/usr/local/nginx/html/下編寫測試文件1.phpcurl
vim 1.php <?php echo "test";
訪問測試tcp
[root@linux-10 html]# curl localhost/1.php test
由於Nginx支持include語法,因此能夠在配置文件中定義虛擬主機配置文件(相似於Apache)工具
刪除原有配置文件中的server部分,增長包含虛擬主機配置文件的規則測試
mkdir /usr/local/nginx/conf/vhost
server { listen 80 default_server; // 有這個標記的就是默認虛擬主機 server_name lem.com; // 指定網站域名 index index.html index.htm index.php; //指定索引頁 root /data/wwwroot/default; //指定網站存放位置 }
[root@linux-10 vhost]# mkdir -p /data/wwwroot/default/
vim index.html This is a default site.
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
注:從新加載相對於重啓服務而言,它在配置文件有錯誤時,將不會從新加載,即不會破壞原有正在運行的Nginx服務。
[root@linux-10 conf]# curl localhost This is a default site. [root@linux-10 conf]# curl -x127.0.0.1:80 123.com This is a default site.
在vhost目錄下,每個虛擬主機配置文件就是一個虛擬主機
vim /usr/local/nginx/conf/vhost/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; } }
生成密碼文件須要藉助Apache的htpasswd工具進行生成,若是本機存在Apache可直接使用,不然須要yum安裝。
yum install -y httpd
生成密碼文件
htpasswd -c /usr/local/nginx/conf/htpasswd lem
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
[root@linux-10 vhost]# curl -x 127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.14.0 Date: Fri, 08 Jun 2018 04:28:28 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth"
location /admin/ //在配置文件中填寫相應目錄 { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
location ~ admin.php //location爲定位語句,匹配指定內容 { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
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; } }
Nginx中支持識別多個域名,無需使用別名
permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302
ctrl+r :-t ctrl+r :-s
[root@linux-10 vhost]# curl -x 127.0.0.1:80 test2.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.14.0 Date: Fri, 08 Jun 2018 04:51:34 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/
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