Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

標籤: Nginx

phpBB 3.2 的 nginx 的 location 設定

Posted on 2017 年 2 月 2 日2021 年 3 月 12 日 By 日落 在〈phpBB 3.2 的 nginx 的 location 設定〉中尚無留言

先說一下 phpBB 安裝工具吧,安裝程式的路徑不是在跟目錄而是在「/install」,entry point 是 app.php,而 app.php 又有自己的 route。

如果只是這樣設定的話,route 會失敗:

location / {
    try_files  $uri /index.php?$uri$args;
}

所以另外對「/install」增加了一個 rule 才能正常執行安裝工具:

location /install {
    try_files  $uri /app.php?$uri$args;
}


安裝完成後,論壇系統的有二個 entry point,所以不能只單設定 index.php,還有 app.php,因此 nginx 的設定要改成下面這樣:

location / {
    try_files  $uri /app.php?$uri$args /index.php?$uri$args;  # 注意順序
}

當然,安裝完成以後就可以把「/install」的設定砍掉了。

Tags:Nginx, PHP, 資訊學習

Nginx upstream 的容錯設定方法

Posted on 2014 年 8 月 22 日2021 年 3 月 12 日 By 日落 在〈Nginx upstream 的容錯設定方法〉中尚無留言

自己的 Redmine server 是使用 Thin 執行,在透過 Nginx 將 request 轉送給 Thin。

Nginx 中設定 Thin server pool:

upstream redmine_thin_servers {
  server unix:/home/redmine/run/thin.0.socket;
  server unix:/home/redmine/run/thin.1.socket;
  server unix:/home/redmine/run/thin.2.socket;
  server unix:/home/redmine/run/thin.3.socket;
}

Nginx server:

  location / {
    try_files $uri/index.html $uri/index.htm @redmine_thin_servers;
  }

  location @redmine_thin_servers {
    proxy_pass http://redmine_thin_servers;
  }

這樣設定,Nginx 會將 request 平均轉送給 4 個 Thin instance。

但若其中一個 Thin instance 處理叫複雜或是較花時間的工作,如 checkout repository 等,會長時間沒有回應,可能沒辦法再同時處理另一個 request。此時若 Nginx 將另一個 request 轉送到已經 pending 的 Thin instance,使用者就會一直等到 Nginx 預設的 proxy timeout 後,出現 HTTP 500 Internal Sever Error 錯誤訊息。但實際上可以讓 Nginx 暫時略過 pending 的 server,將 request 優先轉送給其他 3 個正在 stand by 的 server。

利用 ngx_http_upstream_module 中的 parameter,將 timeout 的 server 標記先移出 pool,讓 request 不會送到 pending 的 server,過一段時間以後再拉進 pool 繼續使用。

以下設定若該 server 超過 10 秒沒有回應,則移出 pool,過 5 分鐘在拉回 pool:

upstream redmine_thin_servers {
  server unix:/home/redmine/run/thin.0.socket fail_timeout=10s slow_start=300s;
  ....
}

另外也可讓 Nginx 主動去檢查 server status,不要在使用者送 request failed 後才將 server 移出 pool。health_check 會讓 Nginx 主動發送間單的 request,透過 HTTP status 來判斷 server 狀態:

  location / {
    try_files $uri/index.html $uri/index.htm @redmine_thin_servers;
  }

  location @redmine_thin_servers {
    proxy_pass http://redmine_thin_servers;
    health_check interval=3s 
  }

測試機上,不曉得是不是 Nginx 1.6.0 還不支援這幾個參數,以上的設定在 configtest 會出現錯誤訊息:

invalid parameter "slow_start=300s"
unknown directive "health_check"

後來是繞路換一個方法。Nginx 的 ngx_http_proxy_module 中,可以設定當 server 多久沒有收到回應 (proxy_read_timeout) 時,直接當作失效並從 pool 中找下一台 server 送 request:

  location / {
    try_files $uri/index.html $uri/index.htm @redmine_thin_servers;
  }

  location @redmine_thin_servers {
    proxy_pass http://redmine_thin_servers;

    # 超過 3 秒沒有收到 response 則當作 timeout
    proxy_read_timeout 3s;

    # 若 response 是 HTTP 50x 也當作 server 失效
    proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
  }
Tags:Nginx

Nginx 將 HTTP 轉向 HTTPS

Posted on 2013 年 10 月 8 日2021 年 3 月 12 日 By 日落 在〈Nginx 將 HTTP 轉向 HTTPS〉中有 1 則留言

看了「Switch to HTTPS Now, For Free」後,到 StartSSL 將手上幾個常用的網域都申請了 SSL 憑證。

SSL 憑證上線使用後,再來就讓任何 HTTP 連線都可以導到 HTTPS。

server {
   location / {
      # rewrite ^ https://my.url;
      rewrite ^ https://$server_name permanent;
   }
}

或者要將網址後半部的參數一起轉過去:

location {
   rewrite ^ https://$server_name$request_uri permanent;
}

note: 「permanent」會發送 HTTP 301

Ref:
Nginx HttpRewriteModule
http://wiki.nginx.org/HttpRewriteModule

Tags:Nginx

URL Rewrite for FuelPHP on Nginx

Posted on 2013 年 9 月 29 日2021 年 3 月 12 日 By 日落 在〈URL Rewrite for FuelPHP on Nginx〉中尚無留言

在 Nginx 設定檔預設值時,FuelPHP 的 URL 看起來是:

http://my.site/index.php/controller/param/

將 URL rewrite 成:

http://my.site/controller/param/

先改 Nginx 設定:

location / {
   try_files  $uri /index.php?$uri$args;
}

這樣能讓 Nginx 處理不含 index.php 的 URL,再來需要修改 FuelPHP 的設定,讓 Uri::create() 等產生的網址自動將 index.php 去掉。

修改 fuel/app/config/config.php:

return arary(
   //'index_file'  => 'index.php',
   'index_file'  => false,
);
Tags:FuelPHP, Nginx

隱藏 HTTP Server 資訊

Posted on 2013 年 6 月 15 日2022 年 7 月 16 日 By 日落 在〈隱藏 HTTP Server 資訊〉中尚無留言
隱藏 HTTP Server 資訊

Apache 和 Nginx 預設頁尾都會加上伺服器資訊,像是:

有些環境或許很忌諱出輸出這類的資料,可以修改設定將他隱藏起來。

Apache conf 中調整 ServerTokens 參數,將頁尾伺服器資訊作不同的調整。設定檔路徑可能會不一樣,可以 grep ServerTokens,以下是 Ubuntu 的路徑。

/etc/apache/conf.d/security:

#ServerTokens OS
ServerTokens Prod

Nginx conf 裡面則是用 server_tokens 參數,將其設定為 off 則不會輸出伺服器版本號。

server {
   server_tokens off;

   ...
}
Tags:Apache, Nginx, Web

文章分頁

上一頁 1 ... 3 4

其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.