Onion-Location是一種可以對網站訪客推播洋蔥站臺的簡易方式。 您可設置網頁伺服器來顯示 Onion-Location 標頭或在網站上增加 HTML <meta>屬性。

下列條件必須要滿足才能夠送出有效的協定表頭:

  • Onion-Location的值必須要是有效的http:或https:協定位址,以及一個以.onion結尾的域名。
  • 定義Onion-Location表頭的網頁必須要透過HTTPS協定傳輸。
  • 定義Onion-Location表頭的網頁不可以放置於洋蔥站臺上。

在這篇教學裡,都是以Debian為基礎的作業系統上的指令來管理網頁伺服器,這些指令在其他系統上可能會有所差異。 請查閱您的作業系統與網頁伺服器的相關文件。

Apache

要在Apache 2.2以上的版本設定此表頭,您必須要啟用headers以及rewrite模組,並且編輯網站的虛擬主機檔案。

第1步 啟用表頭並重寫模組,再重新載入Apache2

 $ sudo a2enmod headers rewrite

 $ sudo systemctl reload apache2

如果您有看到錯誤訊息的話,那就表示有地方做錯了,您必須要先找出問題才能夠繼續往後的步驟。

第2步 在虛擬主機設定檔中加入Onion-Location表頭

Header set Onion-Location "http://your-onion-address.onion%{REQUEST_URI}s"

your-onion-address.onion 為所欲指向的洋蔥地址,而 {REQUEST_URI} 則是 請求網址 URI 的路徑組件, 例如 "/index.html".

虛擬主機示例:

     <VirtualHost *:443>
       ServerName <your-website.tld>
       DocumentRoot /path/to/htdocs

       Header set Onion-Location "http://your-onion-address.onion%{REQUEST_URI}s"

       SSLEngine on
       SSLCertificateFile "/path/to/www.example.com.cert"
       SSLCertificateKeyFile "/path/to/www.example.com.key"
     </VirtualHost>

步驟 3. 重啟 Apache

重新載入 apache2 服務,以讓所設置的變動生效:

 $ sudo systemctl reload apache2 

如果您有看到錯誤訊息的話,那就表示有地方做錯了,您必須要先找出問題才能夠繼續往後的步驟。

步驟 4. 測試 Onion-Location

要測試 Onion-Location 是否奏效,請抓取該網站 HTTP 標頭例如:

 $ wget --server-response --spider your-website.tld

找到 onion-location 這列與洋蔥服務地址。 或在 Tor 瀏覽器開啟這個網站,則網址列會出現一個紫色圈圈。

Nginx

要配置 Onion-Location 標頭,服務營運者需先設定洋蔥服務。

步驟 1. 對 Tor 設定檔 torrc作以下設定來建立洋蔥服務:

HiddenServiceDir /var/lib/tor/hs-my-website/
HiddenServiceVersion 3
HiddenServicePort 80 unix:/var/run/tor-hs-my-website.sock

步驟 2.編輯網站配置檔

/etc/nginx/conf.d/<your-website>.conf 配置檔新增 Onion-Location 標頭與洋蔥服務地址。 例如:

    add_header Onion-Location http://<your-onion-address>.onion$request_uri;

Onion-Location 設置檔應該長這樣:

server {
    listen 80;
    listen [::]:80;

    server_name <your-website.tld>;

    location / {
       return 301 https://$host$request_uri;
    }

}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name <your-website.tld>;

    # managed by Certbot - https://certbot.eff.org/
    ssl_certificate /etc/letsencrypt/live/<hostname>/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/<hostname>/privkey.pem;

    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;
    add_header Onion-Location http://<your-onion-address>.onion$request_uri;

    # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    access_log /var/log/nginx/<hostname>-access.log;

    index index.html;
    root /path/to/htdocs;

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

server {
        listen unix:/var/run/tor-hs-my-website.sock;

        server_name <your-onion-address>.onion;

        access_log /var/log/nginx/hs-my-website.log;

        index index.html;
        root /path/to/htdocs;
}

步驟 3. 測試網站配置

 $ sudo nginx -t

網頁伺服器應確認新語法行得通:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

步驟 4. 重啟 nginx

 $ sudo nginx -s reload

如果您有看到錯誤訊息的話,那就表示有地方做錯了,您必須要先找出問題才能夠繼續往後的步驟。

步驟 5. 測試 Onion-Location

要測試 Onion-Location 是否奏效,請抓取該網站 HTTP 標頭例如:

 $ wget --server-response --spider your-website.tld

找到 onion-location 這列與洋蔥服務地址。 或在 Tor 瀏覽器開啟這個網站,則網址列會出現一個紫色圈圈。

Caddy

Caddy 功能預設自動 HTTPS,它會規定您的 TLS 憑證並將 HTTP 轉向 HTTPS。 如果使用 Caddy2,要包含 Onion-Location 標頭,請在 Caddyfile 加入以下宣告:

header Onion-Location http://<your-onion-address>.onion{path}

如果是靜態網站而且洋蔥地址有 $TOR_HOSTNAME環境變數,則 Caddyfile 將像這樣:

your-website.tld

header Onion-Location http://{$TOR_HOSTNAME}{path}
root * /var/www
file_server

測試它: 測試它和:

 $ wget --server-response --spider your-website.tld

找到 onion-location 這列與洋蔥服務地址。 或在 Tor 瀏覽器開啟這個網站,則網址列會出現一個紫色圈圈。

使用 HTML <meta> 屬性

Onion-Location 的相同行為包括將其定義為 HTML <meta> http-equiv 屬性的選項。 偏好(或必須)透過修改提供的 HTML 內容來定義 Onion-Location,而非新增 HTTP 標頭的網站可使用此方式。 Onion-Location 標頭相當在網頁 HTML head 添加的 <meta http-equiv="onion-location" content="http://<your-onion-service-address>.onion" /> 。 將 <your-onion-service-address.onion> 替換為欲指向的洋蔥服務。

限制

HTML 無法讀取請求的網址,動態插入在 http-equiv <meta> 標籤。 為此,不管訪客在哪個子頁面,都會重新被導向中繼標籤內容指定的 .onion 網址。

因此可以的話,建議使用以下列任一方式。

更多資訊

閱讀 Onion-Location spec.