88
votes
Accepted
What is the difference between Nginx variables $host, $http_host, and $server_name?
You should almost always use $host, as it's the only one guaranteed to have something sensible regardless of how the user-agent behaves, unless you specifically need the semantics of one of the other ...
- 241k
87
votes
nginx url rewriting: difference between break and last
OP preferred an example. Also, what @minaev wrote, was only a part of the story! So, here we go...
Example 1: No (break or last) flags
server {
server_name example.com;
root 'path/to/somewhere'...
- 6,014
30
votes
Accepted
Nginx multiple roots
The configuration has the usual problem that generally happens with nginx. That is, using root directive inside location block.
Try using this configuration instead of your current location blocks:
...
- 36.1k
19
votes
Accepted
Add samesite to cookies using Nginx as reverse proxy
With this code you can define all your application cookies as secure, httponly and/or samesite using proxy_cookie_path (http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cookie_path)
...
- 306
17
votes
Accepted
Apache 2.2 redirect all but one directory
You can modify the Redirect directive to instead make use of a RedirectMatch and use a pattern excluding /specialdir:
RedirectMatch Permanent "^(/(?!specialdir/).*)" https://example.net/$1
- 660
17
votes
Accepted
What is the difference between HTTP_HOST and HTTPS_HOST in apache .htaccess files?
There is no such Apache server variable HTTPS_HOST, only HTTP_HOST. If HTTPS_HOST is set on your server then it's specific to your server.
The HTTP_HOST server variable contains the value of the Host ...
- 12.2k
16
votes
Accepted
Is it possible to replace content on every page passed through a proxy similar to how mod_rewrite is used for URLs?
There's an apache module called mod_substitute that can do this. Here's a short example:
<Location "/">
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s/uat.site.co.jp/jp.uat....
- 27.6k
14
votes
Disabling URL decoding in nginx proxy
Note that URL decoding, commonly known as $uri "normalisation" within the documentation of nginx, happens before the backend IFF:
either any URI is specified within proxy_pass itself, even if just ...
- 13.5k
14
votes
Nginx redirect one path to another
For me it worked without the equals sign like this:
location /old-url {
return 301 /new-url;
}
- 241
12
votes
Remove "www" and redirect to "https" with nginx
This works for me:
server {
listen 80;
server_name www.yourdomain.com yourdomain.com;
return 301 https://yourdomain.com$request_uri;
}
server {
...
- 221
12
votes
Accepted
URL Rewrite not available after installing on IIS 10
I found out that URL Rewrite can be uninstalled via Add or Remove Programs. Apparently its named IIS URL Rewrite Module 2 and not Microsoft URL Rewrite Module which a lot of posts suggest.
After ...
- 263
10
votes
Prevent URL Rewrite rules from being inherited by subdirectories in IIS7
I recently ran into this problem, in a similar situation. But the answer from rjenkins would seem to cause problems with virtual applications that relied on inheritance of parent settings.
If you ...
- 203
10
votes
Accepted
Nginx - populate REQUEST_URI with rewritten URL
$request_uri has the value of the original URI and $uri has the value of the final URI. You could use the set directive to save a snapshot of $uri from inside the location / block and use it later to ...
- 12.4k
9
votes
Apache 2.2 rewrite - Force all requests to index.html
Answer of @prix is good. I myself needed to add conditions so that normal resources (images & css) are served as normal:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{...
- 231
9
votes
Nginx rewrite URL only if file exists
Please don't use if inside a location block. Bad things may happen.
location ~* ^/images/(.+)$ {
root /www;
try_files /path/to/$1 /website_images/path_to/$1 /any/dir/$1 @your404;
}
$1 becomes ...
9
votes
Remove "www" and redirect to "https" with nginx
After spending so much time with hundreds of similar cases, I've come up with
the following snippet. It's short and can be easily tweaked to fit anything.
server {
listen 80;
listen 443 ssl;
...
- 191
9
votes
Is it possible to replace content on every page passed through a proxy similar to how mod_rewrite is used for URLs?
If you haven't restarted Apache, be sure to do that, but if you've already done so, you could try a global output filter that runs a custom PHP script to do your replacing just to see if that solves ...
- 973
9
votes
Accepted
Use of '\' Before DOT in Domain Names in .htaccess Code
Because the second argument to both the RewriteCond and SetEnvIfNoCase directives are regular expressions (regex), not ordinary strings. Regex use a special syntax to define search patterns. In regex ...
- 12.2k
9
votes
Accepted
Nginx simple redirect of products from old to new category
Answering your questions in order...
This
rewrite ^/old-category/(.*) /new-category/$1;
and this
rewrite ^/old-category/(.*)$ /new-category/$1;
as written are equivalent. The .* rule matches 0 or ...
- 2,509
8
votes
Accepted
Redirect traffic based on url to different ip address maintaining port and url information
I have established what the problems were with my setup.
1) SELinux was preventing me from connecting upstream. I have now disabled this and will consider setting it up properly later
2) proxy_pass ...
- 251
8
votes
Accepted
Hide protocol in URL with Nginx?
No, this is entirely a function of the browser and has nothing to do with the HTTP server.
- 32.5k
8
votes
Add samesite to cookies using Nginx as reverse proxy
I had similar problem with web app which doesn't support samesite attribute. I've created similar workaround to @Beccari solution:
proxy_cookie_path ~^/(.+)$ "/$1; SameSite=none";
You have to put it ...
- 185
7
votes
multiple sub_filter rules for a reverse proxy on nginx?
Unlike the previous one, the current version of nginx allows to create multiple sub filter rules under one location directive. Like so
location / {
resolver 8.8.8.8;
proxy_pass http://original-...
- 171
7
votes
Accepted
Nginx rewrite without redirect
nginx is trying to add a trailing / to turn the URI into the correct format for a directory spec. Add the / in your rewrite so that nginx doesn't have to. Try this:
rewrite ^/foo-(.*?)/?$ /bar/$1/ ...
- 12.4k
6
votes
Apache RewriteRule for proxying
Apache 2.4 and later has a directive to remove the X-Forwarded-* headers.
ProxyAddHeaders off
https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxyaddheaders
- 161
6
votes
Nginx Redirect via Proxy, Rewrite and Preserve URL
You can use the following config to have a 100% seamless mapping between /some/path/ on the front-end and / on the backend.
Note that this is the only answer so far that would also seamlessly take ...
- 13.5k
6
votes
Accepted
How do you prevent mod_rewrite from decoding a string in the path?
You don't. The web server is allowed to decode percent-encoded characters which don't decode to special characters before applying rewrite rules, and is allowed to decode the remainder of percent-...
- 241k
6
votes
Using RewriteMap Lookups in a RewriteCond . Possible?
I've discovered that you cannot have a variable in the RewriteCond regex, because it seems it is only compiled once, but not per-request.
You could workaround this by putting both ${lookup} and %{...
- 13.1k
6
votes
What is the difference between Nginx variables $host, $http_host, and $server_name?
I would like to add another important point not mentioned in the accepted answer.
$host do NOT have port number, while $http_host does include the port number.
edit: not always.
I set up a header "...
6
votes
Clean URLs and php extension on nginx
Did you try with the rewrite directive? For example:
location / {
try_files $uri $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =...
- 181
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
rewrite × 2106nginx × 916
apache-2.2 × 393
mod-rewrite × 364
.htaccess × 312
iis × 226
redirect × 206
url × 150
apache-2.4 × 112
reverse-proxy × 106
iis-7 × 92
php × 75
regex × 66
wordpress × 56
rewritecond × 56
proxy × 55
301-redirect × 50
iis-7.5 × 46
virtualhost × 39
https × 39
apache2 × 38
linux × 34
ssl × 34
subdomain × 34
php-fpm × 33