Linux
如何合併這兩個 nginx 規則以使其正常工作?
我在使用 htaccess 規則之前執行 Apache,但切換到執行 Discourse 論壇軟體的 docker 實例的 nginx。我在 docker 實例之外執行 nginx,然後使用 proxypass 使其位於 domain.com/forum 中。在我設置它之前,如下所示,以便將輸入的任何子域減去 www。並將其附加到網址的末尾。如果沒有提供子域,它將最終正常工作。
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 內容的目前程式碼合併?我試圖把它放在那裡,但結果只是一個循環。
任何見解將不勝感激,謝謝!
這就是我最終得到的結果。事後我為特定的子域添加了一些額外的東西,但這就是真正起作用的原因。
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; } }