Nginx proxy_set_header 不適用於 Apache GEOIP

Nginx proxy_set_header 不適用於 Apache GEOIP

我使用 Nginx 反向代理到 apache2 使用以下命令教學

然後我嘗試使用這個將 geoip 安裝到 Nginx教學

反向代理已經完美地工作了一段時間,直到我嘗試安裝 geoip 資料庫以便我可以在 PHP 上取得國家/地區程式碼。

我按照教程的指示在 nginx 上進行了以下配置。

location ~ \.php$ {

    #       location / {
            #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            #include snippets/fastcgi-php.conf;

           proxy_pass http://1.2.3.4:8080$request_uri;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
           proxy_set_header GEOIP_COUNTRY_CODE3 $geoip_country_code3;
           proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;

           proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
           proxy_set_header GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
           proxy_set_header GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
           proxy_set_header GEOIP_REGION $geoip_region;
           proxy_set_header GEOIP_CITY $geoip_city;
           proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;
           proxy_set_header GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
           proxy_set_header GEOIP_LATITUDE $geoip_latitude;
           proxy_set_header GEOIP_LONGITUDE $geoip_longitude;
    include /etc/nginx/proxy_params;
    }

如果我使用 proxy_pass 到 apache2,GEOIP 變數**全部不會顯示在 phpinfo 中

如果我直接使用 nginx fastcgi_pass (關閉 apache 的反向代理),我可以獲得環境變數集,它們反映在 phpinfo 中。

看來 proxy_set_header 可能無法工作,因為 apache 似乎沒有讀取它。

我必須做什麼才能讓 apache 獲得所有變數?

答案1

看來本教學和其他多個從本 geoip nginx 教學複製程式碼的網站是錯誤/過時的。

設定代理頭時,似乎不能使用底線(_)和繆斯使用(-)。所以從改變之後

proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;

對此

proxy_set_header GEOIP-CITY-COUNTRY-CODE $geoip_city_country_code;

apache 能夠取得變數並且現在它也顯示在我的 phpinfo 上。

我的 php 完整位置區塊現在看起來像這樣。

location ~ \.php$ {

           proxy_pass http://1.2.3.4:8080$request_uri;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header GEOIP-COUNTRY-CODE $geoip_country_code;
           proxy_set_header GEOIP-COUNTRY-CODE3 $geoip_country_code3;
           proxy_set_header GEOIP-COUNTRY-NAME $geoip_country_name;

           proxy_set_header GEOIP-CITY-COUNTRY-CODE $geoip_city_country_code;
           proxy_set_header GEOIP-CITY-COUNTRY-CODE3 $geoip_city_country_code3;
           proxy_set_header GEOIP-CITY-COUNTRY-NAME $geoip_city_country_name;
           proxy_set_header GEOIP-REGION $geoip_region;
           proxy_set_header GEOIP-CITY $geoip_city;
           proxy_set_header GEOIP-POSTAL_CODE $geoip_postal_code;
           proxy_set_header GEOIP-CITY-CONTINENT-CODE $geoip_city_continent_code;
           proxy_set_header GEOIP-LATITUDE $geoip_latitude;
           proxy_set_header GEOIP-LONGITUDE $geoip_longitude;
    include /etc/nginx/proxy_params;
    }

透過上述設置,我將能夠獲取以下伺服器變數及其填充的值。請注意,出於安全原因,某些伺服器添加了 HTTP 前綴。不知何故......破折號再次轉換回下劃線。

$_SERVER['HTTP_GEOIP_LONGITUDE'] 
$_SERVER['HTTP_GEOIP_LATITUDE'] 
$_SERVER['HTTP_GEOIP_CITY'] 
$_SERVER['HTTP_GEOIP_REGION'] 
$_SERVER['HTTP_GEOIP_CITY_COUNTRY_CODE'] 
$_SERVER['HTTP_GEOIP_COUNTRY_NAME'] 
$_SERVER['HTTP_GEOIP_COUNTRY_CODE3'] 
$_SERVER['HTTP_GEOIP_COUNTRY_CODE']

相關內容