2015年9月1日 星期二

nginx vhost配置

系統:ubuntu 14.04

1. 建立資料夾
# mkdir /var/www/html/test.vm  # 程式資料夾
# mkdir /var/www/html/test.vm/logs # log資料夾

# chmod 755 -R /var/www/html/test.vm
# chown www-data:www-data -R /var/www/html/test.vm  # 我沒做,依然用bear:bear

2. 建立設定檔
# touch /etc/nginx/sites-available/test.vm
編輯設定檔test.vm內容:
server {
    listen 80;
    listen [::]:80;

    root /var/www/html/test.vm;  # 設定你程式的路徑
    index index.html index.htm; 

    server_name test.vm; # 虛擬主機的完整名稱

    location / {
        try_files $uri $uri/ =404;
    }

    access_log /var/www/html/test.vm/logs/access.log; # access_log 的位置
    error_log /var/www/html/test.vm/logs/error.log;
}

3. 啟用虛擬主機
nginx沒有像Apache 有a2ensite的指令來啟用虛擬主機,所以要用ln 去做
# ln -s /etc/nginx/sites-available/test.vm /etc/nginx/sites-enabled/test.vm

4. 重新啟動nginx
# service nginx restart

5. php頁面打不開(連結變成下載php檔案)
在你的test.vm加入下面這一段
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index  index.php;
        include        fastcgi_params;
}

完整的test.vm檔:
server {
    listen 80;
    listen [::]:80;

    root /var/www/html/test.vm;
    index index.html index.htm;

    server_name test.vm;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index  index.php;
        include        fastcgi_params;
    }

    access_log /var/www/html/test.vm/logs/access.log;
    error_log /var/www/html/test.vm/logs/error.log;

}

5.2 Nginx $document_root$fastcgi_script_name 與 $request_filename 的區別
http://serverfault.com/questions/465607/nginx-document-rootfastcgi-script-name-vs-request-filename  Nginx $document_root$fastcgi_script_name vs $request_filename
http://serverfault.com/questions/502776/what-is-the-correct-setting-of-nginxs-fastcgi-param-script-filename  What is the correct setting of Nginx's fastcgi_param SCRIPT_FILENAME?
$request_filename
This variable is equal to path to the file for the current request, formed from directives root or alias and URI request;
這個變數等於當前請求檔案的路徑,從指向的根或alias和URI請求所形成
$document_root
This variable is equal to the value of directive root for the current request;
這個變數等於當前請求的指向的根
$fastcgi_script_name
This variable is equal to the URI request or, if if the URI concludes with a forward slash, then the URI request plus the name of the index file given by fastcgi_index. It is possible to use this variable in place of both SCRIPT_FILENAME and PATH_TRANSLATED, utilized, in particular, for determining the name of the script in PHP.
這個變數等於URI請求,如果URI的結尾是斜線( ex. http:xxx/xx/ ),則該URI請求會加上fastcgi_index所設的索引名字。
$request_filename is just a nicer way of writing it.
$request_filename 只是更好的寫法

6.1 將入口檔改為a.do( 其內容一樣是php )
index 新增 a.do
index index.html index.htm a.do;
在 location / {} 中加入這段
location ~ .*\.(do|php)?$ {
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME  /your_a.do_path/$fastcgi_script_name;
    include        fastcgi_params;
}

6.1.1 (AWS nginx編譯安裝)出現錯誤:
2016/xx/xx XX:XX:XX [error] 8866#0: *41 open() "/root/project/test.html" failed (13: Permission denied), client: x.x.x.x, server: user.domain.net, request: "GET /test.html HTTP/1.1", host: "user.domain.net"
原因,nginx編譯安裝只能把程式放在 安裝的路徑下 /usr/local/webserver/nginx/html/ 

6.1.2 出現錯誤:
2016/xx/xx XX:XX:XX [error] 8924#0: *42 FastCGI sent in stderr: "Access to the script '/usr/local/webserver/nginx/html/project/a.do' has been denied (see security.limit_extensions)" while reading response header from upstream, client: 1
6.1.2.2 出現錯誤:(CentOS 7)
頁面顯示 Access Denied.
access log 有記錄,error log 無記錄,一樣用下面的解法解。security.limit_extensions那行註解記得拿掉
解法:
http://stackoverflow.com/questions/23390531/access-denied-403-for-php-files-with-nginx-php-fpm
php-fpm的 /etc/php-fpm.d/www.confsecurity.limit_extensions新增.do
security.limit_extensions = .php .php3 .php4 .php5 .do

6.2 如果網頁伺服器是Apache2, 使a.do可以視為php網頁訪問
http://stackoverflow.com/questions/3555681/why-are-my-php-files-showing-as-plain-text
apache/conf/httpd.conf
修改 <IfModule mime_module> 中的
AddType application/x-httpd-php .php => AddType application/x-httpd-php .php .do

7. 設定url rewrite
在 location / {} 中加入這段
rewrite ^/(.+)-(.+).html$ /a.do?controller=$1&action=$2 last;

7.2 apache2
http://stackoverflow.com/questions/13867543/htaccess-rewrite-shtml-and-html-as-php
在網頁根目錄加入.htaccess,內容為
RewriteEngine On
RewriteRule ^(.+)_(.+)\.shtml$ /a.do?controllers=$1&actions=$2 [L,QSA]

8. 設定alias 資料夾
在 server {} 中加入
location /phpmyadmin {
    alias /var/www/phpmyadmin/$1;
}
這樣 http://your_domain/phpmyadmin 就能直接訪問 /var/www/phpmyadmin/ 的phpmyadmin

9. 設定多個網址指到同一隻程式(同Apache的ServerAlias)
在server {} 中的server_name加入
server_name test.vm m.test.vm; # 虛擬主機的完整名稱

參考資料:
http://www.arthurtoday.com/2010/09/ubuntu-nginx-virtual-host.html Ubuntu 設定 Nginx 虛擬主機 ( Virtual Host )
http://stackoverflow.com/questions/5687938/nginx-trouble-loading-index-file nginx trouble loading index file



沒有留言:

張貼留言