nginx location fix: Redirect to index.html - nginx

I'm runnning nginx v 1.0.4 and we're trying to do the following:
location ~ ^/$ {
rewrite  ^.*$  /index.html  last;
}
Basically: If the user gets to the the default domain http://www.foo.com or http://www.foo.com/ redirect them to http://www.foo.com/index.html
When I add this to my conf file, I get the following:
Starting nginx: nginx: [emerg] unknown directive " " in /etc/nginx/myconf.conf
Thanks in advance.

You can just use rewrite function without location
rewrite ^/$ /index.html last;
or for permanent redirect
rewrite ^/$ /index.html permanent;
to rewrite with parameters, e.g. http://www.foo.com/?param=value -> http://www.foo.com/index.html?param=value
rewrite ^/(\?.*)?$ /index.html$1 permanent;

Related

Prestashop 1.7.8.6 multistore nginx rewrite rules

I install the prestashop 1.7 as a multistore and write nginx rewrite rule as
location /shop-1/ {
rewrite ^/shop-1/(.*)$ /$1 last;
try_files $uri $uri/ /index.php?$args;
}
location /shop-2/ {
rewrite ^/shop-2/(.*)$ /$1 last;
try_files $uri $uri/ /index.php?$args;
}
I follow the nginx conf file from https://devdocs.prestashop.com/1.7/basics/installation/nginx/
Now the Q is Parent domain navigating correctly and showing images but multistore redirected corrected to http://example.com/{shop-1 or shop-2} but not showing the images on the multishop urls, getting nginx 404 error on multishop url but same image showing on a parent domain.
example:
http://example.com/shop-1/45-medium_default/skirt.jpg not showing the image
http://example.com/45-medium_default/skirt.jpg showing the image
To explain what happened here I need to refer to nginx request processing phases, a subject not many people actually understand correctly.
Here is a set of rewrite rules being executed at the NGX_HTTP_SERVER_REWRITE_PHASE:
rewrite ^/(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([\w.-]+)/.+\.jpg$ /img/c/$1.jpg last;
As you can see, none of them matches the /shop-1/45-medium_default/skirt.jpg request URI. So on the next turn, during the NGX_HTTP_FIND_CONFIG_PHASE your location /shop-1/ { ... } will be selected to handle the request. Next, after rewrite ^/shop-1/(.*)$ /$1 last; rule being executed, your request URI will be rewritten to /45-medium_default/skirt.jpg, and due to the used last flag on the rewrite directive the NGX_HTTP_FIND_CONFIG_PHASE will be executed again. However the NGX_HTTP_SERVER_REWRITE_PHASE won't be executed again, and a new URI won't be rewritten according to those rules. What you should do instead is to place a rewrite rule at the server level before the ruleset for rewriting image requests:
rewrite ^/(?:shop-1|shop-2)(/.*) $1;
... image URIs rewrite rules here
Note that I don't use last (or break) flag for this rule since the rewrite rules chain should not be terminated after this rewrite will be triggered. None of those two locations that you show in your question will be needed at all.

How can I use nginx to set a vhost and webroot on a URI of an existing vhost?

I want to send a vhost's requests to git.domain1.tld to sub.domain2.tld/git
I suppose that would conflict with overwritten files so how can I get that domain to point to that location?
You can try this:
location / {
proxy_pass https://sub.domain2.tld/git;
}
Or
rewrite ^/$ https://sub.domain2.tld/git permanent;
rewrite ^/(.*)$ https://sub.domain2.tld/git/$1 permanent;

How do I properly add try_url or rewrite for a .html add?

So, coming from the Apache world I am new to configuring NGNIX more than just the root site. I have tried to use an Apache to NGNIX rewrite, but the output doesn't seem to render the pages correctly.
Original .htaccess code:
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
The output doesn't seem to work after reloading the new configuration.
I have been trying different configurations under my location block trying to just add .html and end of the URI/URL. This is for a sub-directory called "wiki" just serving static HTML files.
Here's my current configuration under HTTPS:
root /var/www/html/;
index index.php index.html;
...
location ^~ /wiki/ {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^(.+)/$ $1.html permanent;
}
The alias or path is /var/www/html/wiki
Please advise. I was able to get /wiki/index.html to render, but any other HTML files do not rewrite from wiki/product to product/product.html. I keep getting 404 errors.

Handle index.php and non-www in nginx

I have a nginx config file that currently handles rewriting of requests via index.php for a Zend Framework application. Example code as follows:
server {
listen 80 default_server
etc....
rewrite ^/index\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^/(.*)$ /index.php/$1 last;
}
etc....
}
However, I now need to add a redirect so that any non-www requests for any domain are processed with a 301 to the www equivalent.
I've come across examples such as:
server {
listen 80;
server_name ~^(?!www\.)(?<domain>.+)$;
return 301 $scheme://www.$domain$request_uri;
}
But how would I combine everything so that any non-www are first redirected with a 301 and then rewritten via index.php?
Two separate server blocks (exactly as you have in your question). The first server block is the default, which handles the www prefix domains already, and (presumably) handles the index.php rewrite correctly.
The second server block only matches domain names without the www prefix.
If you wanted to do it the other way around, that is rewrite index.php before you redirect, then just add a rewrite line to the new server block.

Nginx rewrite domain and URLs

I'm configuring nginx with multiple server names, and trying to set up the following rewrite rules
redirect / on old.domain.com to new.domain.com/specific_page.php
redirect old.domain.com/$1 to new.domain.com/$1
In my "server" configuration, I have already the first rewrite condition working, but I cannot find the way to write the second.
if ($host = 'old.domain.com' ) {
rewrite ^/ http://new.domain.com/my-specific/link/list/info.php permanent;
rewrite ^/(.*)$ http://old.domain.com/$request_uri? permanent;
}
Any ideas how to handle easily this scenario? (I realise this might be an unusual setup.)
Actually I managed to solve my problem :
if ($host = 'old.domain.com' ) {
rewrite ^/$ http://new.domain.com/my-specific/link/list/info permanent;
rewrite ^(.*)$ http://old.domain.com$request_uri? permanent;
}
the first rewrite rule ^/$ matches only http://old.domain.com/ and rewrites it to the requested URL
The second rewrite rule ^(.*)$ matches whatever is behind the http://old.domain.com/ and rewrites the domain only.

Resources