205

Simple location prefix matching works for this without using a rewrite rule as long as you specify a URI in the proxy_pass directive: location /foo { proxy_pass http://localhost:3200/; } Notice the additional / at the end of the proxy_pass directive. NGINX will strip the matched prefix /foo and pass the remainder to the backend server at the URI /. ...


94

The absolute most correct way and best practice is usually as follows: location /foo/ { proxy_pass http://localhost:3200/; # note the trailing slash! } Note the dire importance of the trailing slash in proxy_pass, which automatically alters the $uri variable to have the /foo/ on the front-end correspond with / on the backend. No need for an explicit ...


88

The IIS URL Rewrite Module 2.1 for IIS7+ may be your friend. The module can be downloaded from IIS URL Rewrite. Using the URL Rewrite Module and URL Rewrite Module 2.0 Configuration Reference explain how to use the module. Once the module is installed, you can create a host wide redirect using IIS Manager. Select URL Rewrite, Add Rule(s)..., and Blank rule. ...


42

Use openssl s_client piped to openssl x509: $ openssl s_client -connect foo.example.com:443 < /dev/null | openssl x509 -text (Add -servername foo.example.com to the s_client command if the server uses SNI.) The redirection of stdin from /dev/null for the first invocation of openssl will prevent it from hanging waiting for input.


25

The Order, Deny, and Allow options have been replaced in Apache 2.4 with <Directory /var/www/mysite.com/htdocs/public> Require all granted </Directory> You can explicitly restrict addresses through the use of the following: <Directory /var/www/mysite.com/htdocs/public> Require all granted Require not ip 192.168.0.1 </...


23

Since nginx 0.8.25 named captures can be used in server_name. You should use them. Here, the sub-domain will be stored in a variable called $sub. Then you will be able to reuse it in the rewrite directive : server { listen 80; server_name ~^(?<sub>\w+)\.olddomain\.com$; rewrite ^ $scheme://$sub.doma.in$request_uri? permanent; } Alternatively, ...


21

A redirect from http://old.example.com to https://new.example.com does not require a certificate for old.example.com. But a redirect from https://old.example.com to https://new.example.com does. If people's bookmarks or search engine search results or other external links point to the https site, you better renew the cert. If you merely assume that people ...


17

Yes, you will need a new certificate if the redirection is done in a HTTP response (a 301 or 302 return code). If you don't the redirect will not work, visitors of the old domain will get an error the certificate expired if they visit the old domain via HTTPS.


13

And voila, the fix: <VirtualHost mysite.com:80> ServerName mysite.com ServerAlias www.mysite.com DocumentRoot /home/rotate/public_html ServerAdmin me@mysite.com UseCanonicalName Off </VirtualHost> NameVirtualHost mysite.com:80 <VirtualHost 192.168.1.1:80> ServerName 192.168.1.1 Redirect 403 / ErrorDocument ...


13

In Firefox 57, if you open the Developer Tools and go to the Network tab: Make sure Persist Logs is checked Visit the URL of interest Click on the top row (i.e., the one corresponding to the request to the server you're interested in, which resulted in the redirect response) Click on the Security tab (half-way down, still within Network) This will let you ...


13

There is a special kind of processing for this scenario, as per the docs: If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass, then the special processing is performed. In response to a request with URI equal to this string, ...


9

The answer could be much much simpler. Just copy this into bottom of httpd.conf (usually located at /etc/httpd/conf) <VirtualHost *:80> ServerName localhost Redirect 403 / UseCanonicalName Off UserDir disabled </VirtualHost> <VirtualHost *:80> ServerName www.example.com DocumentRoot /var/www/html </VirtualHost> Then only when ...


8

Yes, you can ABSOLUTELY do it with NGINX! Simply install an exception handler, a.k.a. error_page, to post-process required responses. Make sure to set it in such a way as to prevent the error page from modifying the HTTP Status Code, e.g., don't use the = parameter (or use it to hardcode whichever code you desire). Make sure to return a response with a ...


8

This configuration will do what you want: server { listen 80; server_name example.com; return 301 https://$server_name/subdirectory; } server { listen 443; server_name example.com; location = / { return 301 https://$server_name/subdirectory; } } The = / specifiers means a full match, so it matches only the exact root ...


8

As it stands, you're redirecting all traffic to https, which is good for http traffic, but for https traffic is quite pointless, and results in a redirection loop. What is happening right now is the following: http -> https -> https -> https -> https -> https ... and so on for quite a bit, up until the point where your browser tells you, "it's enough, we won'...


6

Second way is better... server { listen 80; server_name www.domain.com; return 301 $scheme://domain.com$request_uri; } Why Let me quote directly from the official Nginx wiki at Pitfalls and Common Mistakes: By using the built-in variable $request_uri, we can effectively avoid doing any capturing or matching at all, and by using the return ...


6

By default, nginx will return a 503 service temporarily unavailable error code. The limit_req_status directive exists to change the error code in case they hit the limit_req : location = /search/bulk { limit_req zone=one burst=2; limit_req_status 404; } The problem is that this directive only allows a range from 400 to 599, so you ...


6

You have a mistake in your nginx configuration: error_page 403 = 404; This causes nginx to try to redirect to a document named "404", which is exactly what is happening. This should have been written as: error_page 403 =404; Or better yet, it should not be present at all. Sending a blatantly wrong error code is a good way to confuse people (like ...


6

Right now any and all requests are going to hit this server block: server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name _; # This doesn't do anything rewrite ^ $scheme://www.example.com$request_uri permanent; # Rest of file irrelevant } Because: no server block has a valid server_name (...


6

Also, there is a graphical tool for Windows with detailed text trace: SSL Certificate Verifier Tool and tool description: Verifying The SSL Certificates with a tool and here is an example of how it handles redirects:


6

Use Order,Deny and allow to specify who has access to your vhost or location. <VirtualHost *:80> ServerName example.net DocumentRoot /docroot <Directory "/docroot"> Order Deny,Allow Deny from all Allow from 10.10.10.10 Allow from 10.10.11.0/24 </Directory> </VirtualHost> When it ...


5

Question 1: Is that a good idea or is there anything better? Question 2: How to create rules to do this successfully with enough flexibility so I don't have to write many rules every time a new robot hits my site? The better fix, which also coincidentally solves #2 as well, is to fix your application such that it does not throw exceptions when it gets ...


5

The $server_name refers to the server name you have defined in the virtual host block. Therefore your additional block causes a redirect cycle redirecting back to itself. You have to use a literal domain name there instead of a variable. For SSL domain with www, you have to add listen 443 ssl; to the block and certificate values. So, this should be your ...


5

it returns addresses that include "_". server_name _; location /.secret { return http://$server_name$request_uri; } $server_name returns assigned server_name to the server block, which in your case is _; hence the _ in returned address. If you want it to return a hostname or your Host request header, try using $host, something like: location /...


4

No, this would require that you change the behavior of the browser. It's all request response based. The user types example.com in his browser bar and the browser automatically adds http:// in front of that. So your server will always get the first request on http://example.com and you can only answer with a redirect to your SSL enabled address if there is ...


4

You seem to be looking for something like this: if ($http_host ~ (.*)\.olddomain\.com) { set $subdomain $1; rewrite (.*)$ http://$subdomain.newdomain.com$1 permanent; } rewrite ^(/)(.*)$ http://newdomain.com/$2 permanent; These are my testcases $ curl -I -H "Host: test1.olddomain.com" nginx1.tst HTTP/1.1 301 Moved Permanently Server: nginx/1.4.4 ...


4

Don't point at an external url Apache's docs say: The syntax is, ErrorDocument <3-digit-code> <action> where the action can be, Text to be displayed. Wrap the text with quotes ("). An external URL to redirect to. A local URL to redirect to. Pointing at an external url will cause an actual redirect, whereas using a local url is internal. ...


4

My working configuration for this is: server { listen 80; listen [::]:80; server_name example.com www.example.com; root /srv/www/empty; include includes/letsencrypt; location / { return 301 https://www.example.com$request_uri; } } where /etc/nginx/includes/letsencrypt is ...


4

No, DNS is not aware of http, so you can't point it at a server based on URL. You can point DNS at a load balancer, such as Nginx, and have it proxy the request to other servers. If you want redundancy you could look into two A records for the load balancer and run two of them in round robin. In theory, if one load balancer is down the other could be used, ...


4

With ~* you are attempting to loosely match the string, but your regex is ^XYZ$ which says match only if it starts with XYZ and only if it ends in XYZ. That's not loose at all. You are kind of contradicting yourself there. See documentation here: http://nginx.org/en/docs/http/ngx_http_core_module.html#location You probably want to adjust your regex to ...


Only top voted, non community-wiki answers of a minimum length are eligible