cd /usr/local/src wget http://nginx.org/download/nginx-1.12.2.tar.gz tar xf nginx-1.12.2.tar.gz cd nginx-1.12.2 ./configure --prefix=/usr/local/nginx //這裏故意不加其餘參數, 之後再編譯, 正常要根據本身的實際需求加上須要編譯的參數, 如 --with-http_ssl_module make && make install
vim /etc/init.d/nginx //複製以下內容php
參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginxhtml
chmod 755 /etc/init.d/nginx chkconfig --add nginx chkconfig nginx on cd /usr/local/nginx/conf/ && mv nginx.conf nginx.conf.bak
vim nginx.conf //寫入以下內容linux
參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.confnginx
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; //定義pid worker_rlimit_nofile 51200; //nginx 最多能夠打開多少個文件 use epoll; //使用epoll模式 worker_connections 6000; //進程最大鏈接數 每一個server對應着1個虛擬主機 若是想php監聽的是ip 則寫成: /usr/local/nginx/sbin/nginx -t /etc/init.d/nginx start netstat -lntp | grep 80 測試php解析 vim /usr/local/nginx/html/1.php //加入以下內容 <?php echo "test php scripts."; ?> curl localhost/1.php
去掉 usr/local/nginx/conf/nginx.conf 中 的內容 Server { ..... } vim /usr/local/nginx/conf/nginx.conf //增長 include vhost/*.conf; //注意;號別漏, 在conf目錄下加個vhost目錄 mkdir /usr/local/nginx/conf/vhost cd !$ vim default.conf //加入以下內容 server { listen 80 default_server; // 有這個標記的就是默認虛擬主機 server_name 120.com; index index.html index.htm index.php; root /data/wwwroot/default; } mkdir -p /data/wwwroot/default/ echo "This is a default site." >/data/wwwroot/default/index.html /usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload curl localhost curl -x127.0.0.1:80 120.com
vim /usr/local/nginx/conf/vhost/test.com.conf//寫入以下內容git
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; } } yum install -y httpd htpasswd -c /usr/local/nginx/conf/htpasswd aming -c 建立 -m md5加密, 第二次用不用 -c建立了 mkdir /data/wwwroot/test.com echo "test.com">/data/wwwroot/test.com/index.html curl -x127.0.0.1:80 test.com -I //狀態碼爲401說明須要驗證 curl -uaming:passwd -x127.0.0.1:80 test.com -I 訪問狀態碼變爲200 passwd改成本身的aming的密碼 -t && -s reload //測試配置並從新加載 編輯windows的hosts文件,而後在瀏覽器中訪問test.com會有輸入用戶、密碼的彈窗 針對目錄的用戶認證 location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } mkdir /data/wwwroot/test.com/admin echo "admin in test.com">/data/wwwroot/test.com/admin/index.html -t && -s reload //測試配置並從新加載 針對單個文件 location ~ admin.php // ~(.*)admin.php$ 更全面匹配 包含 admin php結尾的文件
更改test.com.confvim
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; } } permanent 301的意思, 想改爲302用 redirect server_name後面支持寫多個域名,這裏要和httpd的作一個對比 permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302 測試 curl -x127.0.0.1:80 test1.com/1.php -i