在使用 htaccess 规则之前,我运行的是 Apache,但后来切换到运行 Discourse 论坛软件的 docker 实例的 nginx。我在 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;
}
}