Nginx

nginx代理域到url,顯示舊域

  • November 13, 2017

我有兩個子域,我想通過以下方式連結: https://suba.example.org/是我的主要子域,https://subb.example.org/是我的輔助子域。在 suba 上,我有一個執行 Web 應用程序的伺服器, subb 僅用於重定向。suba 上的這個伺服器有一個 url,讓我們呼叫它https://suba.example.org/foo.php/bar

我想要的是,每當我https://subb.example.org/在瀏覽器中輸入內容時,https://suba.example.org/foo.php/bar都會顯示 的內容,URL 需要顯示https://subb.example.org/.

截至目前,它將正確顯示內容,https://suba.example.org/foo.php/bar但瀏覽器將顯示內容的 URL 而不是https://subb.example.org/。我已經多次重啟了 nginx-server 並使用了一個隱身視窗,可以肯定的是,瀏覽器不會記憶體任何數據。

正在發生的事情是:

  • https://subb.example.org/在桌面上的 Chrome 隱身視窗中打開:它將顯示正確的輸出https://suba.example.org/foo.php/bar顯示https://suba.example.org/foo.php/bar在 URL 欄中
  • https://subb.example.org/在任何其他瀏覽器/機器上打開(包括另一台機器上的 Chrome 隱身視窗):它將顯示來自URL 欄的內容,https://suba.example.org/foo.php/bar只有文本,沒有 CSS 或圖片。就像一個 80 年代的網站。https://subb.example.org

在點擊進入之前:https://i.stack.imgur.com/Q5mgx.png

發生了什麼:https://i.stack.imgur.com/CfHM1.png

應該發生什麼:https://i.stack.imgur.com/5OxQ4.png

任何幫助表示讚賞。

附件是我的 nginx 站點配置subb

server {
listen 443 ssl;

root /config/www;
index index.html index.htm index.php;

### Server Name
server_name subb.example.org;

### SSL Certificates
ssl_certificate /config/keys/letsencrypt/fullchain.pem;
ssl_certificate_key /config/keys/letsencrypt/privkey.pem;

### Diffie–Hellman key exchange
ssl_dhparam /config/nginx/dhparams.pem;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';

### Extra Settings
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;

### Add HTTP Strict Transport Security
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
add_header Front-End-Https on;

client_max_body_size 0;

location / {
proxy_pass https://suba.example.org/foo.php/bar;
}
}

這是 nginx 站點配置suba

server {
listen 443 ssl;

root /config/www;
index index.html index.htm index.php;

### Server Name
server_name suba.example.org;

### SSL Certificates
ssl_certificate /config/keys/letsencrypt/fullchain.pem;
ssl_certificate_key /config/keys/letsencrypt/privkey.pem;

### Diffie–Hellman key exchange
ssl_dhparam /config/nginx/dhparams.pem;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';


### Extra Settings
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;

### Add HTTP Strict Transport Security
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
add_header Front-End-Https on;

client_max_body_size 0;

location / {
proxy_pass https://192.168.178.6:444/;
}
}

這是響應curl -i https://subb.example.org

HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Mon, 07 Aug 2017 19:24:03 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 11185
Connection: keep-alive
X-Powered-By: PHP/7.1.5
Set-Cookie: oc367h1rrnkw=i7l0tko9m9unbifqus6lqua1v2; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Set-Cookie: oc_sessionPassphrase=eFmSS9gKBYJ4YP0MHDFhmxnhJZnmWTDAMjN4zkTrEenumTa66yy6SeWCs12oU2k2MbDN424ySgGeyyYbciCK7Fs3gmmjtwAJU3a3r87BXZ1Uk%2FmdLEXuZoFdy4mbPH67; path=/; secure; HttpOnly
X-Frame-Options: SAMEORIGIN
Set-Cookie: __Host-nc_sameSiteCookielax=true; path=/; httponly;secure; expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=lax
Set-Cookie: __Host-nc_sameSiteCookiestrict=true; path=/; httponly;secure; expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=strict
Cache-Control: no-cache, no-store, must-revalidate
Content-Security-Policy: default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';frame-src 'self'
Strict-Transport-Security: max-age=15768000; includeSubDomains; preload;
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Robots-Tag: none
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Strict-Transport-Security: max-age=63072000; includeSubdomains
Front-End-Https: on
Strict-Transport-Security: max-age=63072000; includeSubdomains
Front-End-Https: on

我將 Live HTTP Header Plugin 的輸出粘貼到 Pastbin: pastebin.com/a6kfqhMn

解決方案是將對 css、圖形等的請求重定向到https://suba.example.org. 因此,我的站點配置https://subb.example.org如下:

server {
listen 443 ssl;

root /config/www;
index index.html index.htm index.php;

### Server Name
server_name https://subb.exmaple.org;

### SSL Certificates
ssl_certificate /config/keys/letsencrypt/fullchain.pem;
ssl_certificate_key /config/keys/letsencrypt/privkey.pem;

### Diffie–Hellman key exchange
ssl_dhparam /config/nginx/dhparams.pem;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';


### Extra Settings
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;

### Add HTTP Strict Transport Security
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
add_header Front-End-Https on;

client_max_body_size 0;

location ^~ /core/ {
proxy_pass https://suba.exmaple.org;
}

location ^~ /apps/ {
proxy_pass https://suba.exmaple.org;
}

location ^~ /index.php/ {
proxy_pass https://suba.exmaple.org;
}

location / {
proxy_pass https://suba.exmaple.org/foo.php/bar;
}
}

在這裡,帶有的部分location ^~ ...是缺少的。我知道這不是最佳實踐,因為我必須查看整個原始碼才能找到缺少的內容以及我必須重定向的內容。也許有一天有人會閱讀這些行並告訴我重定向 Web 伺服器真實內容的更好方法。但在那之前,這個解決方案將起作用。

引用自:https://serverfault.com/questions/867353