在使用 htaccess 規則之前我執行的是 Apache,但切換到 nginx 運行 Discourse 論壇軟體的 docker 實例。我在 docker 實例之外運行 nginx,然後使用 proxypass 使其位於 domain.com/forum 中。在我進行如下設定之前,為了將輸入的任何子網域減去 www.並將其附加到 url 的末尾。如果沒有提供子網域,它將正常工作。
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} .example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/forum/t/%1$1 [L,NC,QSA]
我使用了從 htaccess 到 nginx 的轉換器,它給了我這個:
location / {
if ($http_host !~ "^www\."){
rewrite ^(.*)$ http://example.com/forum/t/%1$1 redirect;
}
}
我目前的位置設定如下所示:
location /forum {
proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
fastcgi_read_timeout 60;
}
所以我的問題是,如何將 if 語句中轉換後的程式碼與包含 proxy_pass 內容的目前程式碼合併?我試著把它放在那裡,但最後卻出現了一個循環。
任何見解將不勝感激,謝謝!
答案1
這就是我最終得到的效果。我事後為特定的子網域添加了一些額外的東西,但這才是真正起作用的原因。
server {
server_name example.com www.example.com;
listen ipaddress;
root /home/me/public_html;
index index.html index.htm index.php;
access_log /var/log/virtualmin/example.com_access_log;
error_log /var/log/virtualmin/example.com_error_log;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME /home/me/public_html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT /home/me/public_html;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS $https;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/php-nginx/15187383894597.sock/socket;
}
listen 443 ssl http2; listen [::]:443 ssl http2;
ssl_certificate /home/me/ssl.combined;
ssl_certificate_key /home/me/ssl.key;
http2_idle_timeout 5m; # up from 3m default
location /forum {
proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
fastcgi_read_timeout 60;
}
# This server forwards requests from subdomains
server {
server_name ~^(.*)\.example\.com$;
listen ipaddress default_server;
location / {
return 301 https://example.com/forum/c/$1;
}
}
然後我添加了其中的一些來測試隨機的特定的,它們工作得很好。
# Test for specificsub
server {
server_name specificsub.example.com;
listen ipaddress;
location / {
return 301 https://example.com/forum/c/assets/specificsub;
}
}
# Test for specificsub2
server {
server_name specificsub2.example.com;
listen ipaddress;
location / {
return 301 https://example.com/forum/c/assets/specificsub2;
}
}
答案2
像這樣的東西應該可以完成工作。這可能不會完全按照原樣工作,但應該為您提供一個發展自己的整體策略。如果您希望有人為您完成所有這些工作並進行測試,您應該聘請顧問。
我假設您有無限的子域集。
// This server serves only forum traffic on the main domain
server {
server_name example.com;
location /forum {
proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
proxy_set_header Host $http_host;
// etc
}
// This should forward all requests to the forum subfolder
location / {
return 301 http://example.com/forum/;
}
// Insert any other required locations
}
// This server forwards requests from subdomains
server {
server_name ~^(.*)\.example\.com$;
// If the line above doesn't work uncomment these two and try them instead. They basically listen to everything not specified explicitly.
// listen 80 default_server;
// server_name _;
location / {
return 301 http://example.com/forum/t/$1;
}
}