我有一个 NGINX 配置,它使用正则表达式转换 URL,这很好用。问题是,在转发到新的 Atlassian 实例时,URL 中的 + 会变成 %20,这在应用程序中本身是有意义的。
user nginx;
worker_processes auto;
worker_rlimit_nofile 8192;
pid /run/nginx/nginx.pid;
events {
worker_connections 4000;
}
error_log /var/log/nginx/error.log error;
http {
server {
listen 443 ssl default_server;
server_name localhost;
ssl_certificate /etc/nginx/certs/REDACTED.cert;
ssl_certificate_key /etc/nginx/certs/REDACTED.key;
location / {
set $unencoded_args on;
set $substitute_url '';
if ($request_uri ~* "https:\/\/(confluence|public)\.REDACTED\.nl\/display\/(.*)\/(.*)") {
set $unencoded_arg_substitute_url 'https://REDACTED.atlassian.net/wiki/pages/viewpage.action?spaceKey=$2&title=$3';
rewrite ^ $unencoded_arg_substitute_url redirect;
}
if ($request_uri ~* "https:\/\/(confluence|public)\.REDACTED\.nl\/pages\/viewpage.action\?spaceKey=(.*)&title=(.*)") {
set $unencoded_arg_substitute_url 'https://REDACTED.atlassian.net/wiki/pages/viewpage.action?spaceKey=$2&title=$3';
set $substitute_url $unencoded_arg_substitute_url;
rewrite ^ $substitute_url permanent;
}
if ($request_uri ~* "https:\/\/confluence\.REDACTED\.nl\/pages\/viewpage.action\?pageId=(.*)") {
set $unencoded_arg_substitute_url 'https://REDACTED.atlassian.net/l/cp/VjEHCLZv';
rewrite ^ $unencoded_arg_substitute_url redirect;
proxy_pass $unencoded_arg_substitute_url;
}
if ($substitute_url = '') {
set $unencoded_arg_substitute_url 'https://REDACTED.atlassian.net/wiki';
rewrite ^ $unencoded_arg_substitute_url redirect;
}
proxy_pass $unencoded_arg_substitute_url;
proxy_set_header Host REDACTED.atlassian.net/wiki;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
root /var/www;
index index.html;
error_page 502 503 504 fallback.html;
error_page 404 not_found.html;
error_page 401 unauthorized.html;
}
}
}
这是我目前的代码
答案1
要保留 URL 中的 + 字符,可以使用带有 break 标志的重写指令。以下是修改配置以执行此操作的方法:
server {
listen 443 ssl default_server;
server_name localhost;
ssl_certificate /etc/nginx/certs/REDACTED.cert;
ssl_certificate_key /etc/nginx/certs/REDACTED.key;
location / {
set $unencoded_args on;
set $substitute_url '';
if ($request_uri ~* "https:\/\/(confluence|public)\.REDACTED\.nl\/display\/(.*)\/(.*)") {
set $unencoded_arg_substitute_url 'https://REDACTED.atlassian.net/wiki/pages/viewpage.action?spaceKey=$2&title=$3';
rewrite ^ $unencoded_arg_substitute_url redirect;
}
if ($request_uri ~* "https:\/\/(confluence|public)\.REDACTED\.nl\/pages\/viewpage.action\?spaceKey=(.*)&title=(.*)") {
set $unencoded_arg_substitute_url 'https://REDACTED.atlassian.net/wiki/pages/viewpage.action?spaceKey=$2&title=$3';
set $substitute_url $unencoded_arg_substitute_url;
rewrite ^ $substitute_url permanent;
}
if ($request_uri ~* "https:\/\/confluence\.REDACTED\.nl\/pages\/viewpage.action\?pageId=(.*)") {
set $unencoded_arg_substitute_url 'https://REDACTED.atlassian.net/l/cp/VjEHCLZv';
rewrite ^ $unencoded_arg_substitute_url redirect;
proxy_pass $unencoded_arg_substitute_url;
}
if ($substitute_url = '') {
set $unencoded_arg_substitute_url 'https://REDACTED.atlassian.net/wiki';
rewrite ^ $unencoded_arg_substitute_url break;
return 301 $unencoded_arg_substitute_url;
}
proxy_pass $unencoded_arg_substitute_url;
proxy_set_header Host REDACTED.atlassian.net/wiki;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
root /var/www;
index index.html;
error_page 502 503 504 fallback.html;
error_page 404 not_found.html;
error_page 401 unauthorized.html;
}
}
通过添加休息标记到改写指令位于“if ($substitute_url = '')”块中,您应该能够保留 URL 中的 + 字符。后续的 return 301 行执行重定向并保留 + 字符。
希望这对您有用。