12.6 Nginx安裝
- cd /usr/local/src
- wget http://nginx.org/download/nginx-1.12.1.tar.gz
- tar zxf nginx-1.12.1.tar.gz
- ./configure --prefix=/usr/local/nginx //編譯,根據需求,加上相應的參數模塊,源碼包儘可能保留,有些模塊在源碼包裏
- make && make install
- vim /etc/init.d/nginx //複製以下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx ) 啓動腳本
- /usr/local/nginx/sbin/nginx -t /檢查
- cd /usr/local/nginx/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 那個用戶來啓動服務
- worker_processes 定義子進程有幾個
- worker_rlimit_nofile 最多訪問多少個文件
- worker_conections 最大的鏈接數
- 60行,server 相似虛擬主機
- /usr/local/nginx/sbin/nginx -t
- /etc/init.d/nginx start
- ps aux |grep ngnix //ss父進程 S子進程
- curl localhost
- vi /usr/local/nginx/html/1.php //加入以下內容
<?php echo "test php scripts."; ?>php
- curl localhost/1.php
12.7 默認虛擬主機
- vim /usr/local/nginx/conf/nginx.conf //刪除server,http下增長 include vhost/*.conf
- mkdir /usr/local/nginx/conf/vhost
- cd 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; }
- cd vhost
- mkdir /data/wwwroot
- vim index.html This is the fault index
- /usr/local/ngnix/sbin/ngnix -t
- /usr/local/ngnix/sbin/ngnix -s reload /從新加載
- curl -x137.0.0.1:80 aaa.com
- vhost目錄下第一個爲默認虛擬主機
- 加標誌位default_server
12.8 Nginx用戶認證
- 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 / //‘/’根目錄名,能夠換成其餘的目錄/admin或者匹配針對一個url- admin.php { auth_basic "Auth"; //定義用戶的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; 用戶密碼 } } 2. yum install -y httpd 3. htpasswd -c /usr/local/nginx/conf/htpasswd aming //生成密碼 4. -t && -s reload //測試配置並從新加載 5. curl -uaming:lishiming -x127.0.0.1:80 test.com 6. mkdir /data/wwwroot/test.com 7. echo 「test.com」>/data/wwwroot/test.com/index.html 8. curl -uaming:lishiming -x127.0.0.1:80 test.comhtml
12.9 Nginx域名重定向
- 更改test.com.conf 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; //定義主域名 } }
- server_name後面支持寫多個域名,這裏要和httpd的作一個對比
- permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302
擴展
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/1711943linux