HAProxy 搭配 CodeIgniter 取使用者真實 IP

前端 Load Balance 首選就是 HAProxy,後端架設 Nginx 搭配 CodeIgniter,紀錄使用者 IP 時,Nginx 總是只有抓到內部 IP 192.168.x.x,而無法抓到真實 Public IP,要抓到 Public IP 必須修改 HAProxy + Nginx 設定檔,HAProxy 只要在 Backend 地方加入 forward 選項,這樣 HAProxy 會送 X-Forwarded-For header 給後端 Nginx。

option forwardfor
[Read More]

Nginx 搭配 Laravel PHP Framework 設定

Laravel PHP Framework
筆記在

Nginx 設定 Laravel 專案,現在的 PHP Framework 都將 query string 整個導向首頁 index.php,就拿 CodeIgniter 來說,在 Apache 只要設定

RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|robots\.txt|$)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
在 Nginx 內只要透過

try_files 即可

location / {
    try_files $uri $uri/ /index.php
}
正常來說 Laravel 直接用上面的設定即可,但是我發現在 $_GET 這全域變數會拿到空值,解法也很簡單,在 Nginx 將 query string 變數帶到 index.php 後面即可
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

Install Nginx + spdy module on Ubuntu or Debian

nginx-logo
上一篇提到

nginx 1.4.0 釋出並支援 SPDY,教學環境是 CentOS,這次在 Ubuntu 環境編譯遇到

/usr/bin/ld: cannot find -lperl 找不到 perl library,解法可以透過 aptitude 安裝 libperl5.14,安裝好後,到 /usr/lib 底下找到 libperl.so.5.14.2,由於檔案命名關係,請用 ln 將檔案 link 成 libperl.so

$ ln -s libperl.so.5.14.2 libperl.so
接著可以正確編譯了,底下安裝相關套件
$ aptitude -y install libpcre3-dev libgd-dev libgd2-xpm-dev libgeoip-dev

nginx 1.4.0 釋出並支援 SPDY

Update: 由於 OpenSSL CVE-2014-0160 Heartbleed Security,請將 openssl 升級到 1.0.1g 版本

nginx-logo

很高興看到 Nginx Release 1.4.0 版本,此新版本開始支援 SPDY module,對於 SPDY 還不熟悉了解的,可以參考今年 2013 WebConf 講師 ihower 介紹的 A brief introduction to SPDY - 邁向 HTTP/2.0,要提升整個 Web Performance,可以將 SPDY 導入,對於使用 Apache 使用者,請參考 mod_spdy,如果是 Nginx 用戶,在 1.3.x 版本,可以直接用官方 patch,升級到 1.4.x 就不需要 patch 了,但 OS 是 UbuntuCentOS 系列是需要自行編譯,這次會筆記在 CentOS 下將 spdy 編譯進系統。

安裝方式

先看 Ngix 是否有支援 spdy,直接下 nginx -V 觀看

nginx version: nginx/1.1.19 TLS SNI support enabled configure arguments: –prefix=/etc/nginx –conf-path=/etc/nginx/nginx.conf –erth=/var/lib/nginx/body –http-fastcgi-temp-path=/var/lib/nginx/fastcgi –http-lb/nginx/proxy –http-scgi-temp-path=/var/lib/nginx/scgi –http-uwsgi-temp-path==/var/run/nginx.pid –with-debug –with-http_addition_module –with-http_dav_moith-http_image_filter_module –with-http_realip_module –with-http_stub_status__xslt_module –with-ipv6 –with-sha1=/usr/include/openssl –with-md5=/usr/includ/buildd/nginx-1.1.19/debian/modules/nginx-auth-pam –add-module=/build/buildd/d/nginx-1.1.19/debian/modules/nginx-upstream-fair –add-module=/build/buildd/ng

[Read More]

Nginx 判斷檔案是否存在

如果你有在使用 CodeIgniter + Nginx + PHP-FPM 使用者,務必看到這篇 CLI problem nginx php-fpm,在使用 CLI 時候會有些問題,解決方式也非常簡單,只要在 index.php 裡面加上

$_SERVER['PATH_INFO'] = NULL;
當然這篇最主要不是講這個,而是最後我有提到一篇解法,在

Nginx 裡面如何設定 rewrite 功能,比較不同的是,現在不用在設定這麼複雜了,要判斷檔案是否存在,不要在使用下面方式

server {
  root /var/www/domain.com;
  location / {
    if (!-f $request_filename) {
      break;
    }
  }
}
而必須改成
location / {
    try_files $uri $uri/ /index.php;
}
請看

Check IF File Exists,看完之後可以拿掉很多設定,讓 Nginx 設定檔看起來更簡單容易。 參考: try_files

PHP MVC Framework 搭配 Nginx + PHP-FPM 設定檔

Laravel PHP Framework
相信大家都知道

Nginx 搭配 PHP-FPM 用起來效能還不錯,這次來筆記如何設定 Nginx 去除 PHP MVC Framework 討厭的 index.php 字串,不管是 LaravelCodeIgniter 教學文件都是在 Apache 設定 .htaccess 來達成 Cleaner URL,Apache 最大好處支援 .htaccess,但是 Nginx 也有強大的效能,此篇紀錄如何設定 Nginx 達成 mod_rewrite 效果。

[Read More]

在 Ubuntu 10.10 (Maverick) 架設 Nginx + PHP FastCGI

logo-Ubuntu
今天來筆記如何在

Ubuntu 底下完整安裝 Nginx + PHP FastCGI,以及了解 Nginx 基本設定。我想大家都知道 Apache 是一個很好的 Web Server 伺服器,也常常用在個人網站,或者一般小型專案,網路上也有一堆懶人包,如 Appserv, Xampp,對於新手入門來說 Apache 是一個很好的選擇,但是您會發現用了 Apache 後,系統記憶體常常飆高 XD,載入太多額外不必要的模組,所以非常肥大,那這次就來嘗試另外一套 Web 伺服器 Nginx 吧。

[Read More]