I have my domain config file setup for Nginx. I have set domain like www.example.com and I have all sub directory also inside that like basefolder/index.html, basefolder/abc1/signup/index.html, basefolder/abc2/signup/index.html, basefolder/signup/index.html etc as n number can be exists.
I can directly call them through domain/dir/ which will call the index.html file available in that directory. I have some specific query string set for the "signup" folders only like "www.example.com/signup/?query=a12seddf" or "www.example.com/abc1/signup/?query=a12seddf". I want to covert them to the path like "www.example.com/signup/a12seddf" or "www.example.com/abc1/signup/a12seddf". I tried with the rewrite rule but it works only if I give the whole location till the signup but I want to write the single rule which will work on all signup folder instead of writing the rewrite rule for all.
I tried below code but it won't work for me and giving 404 on accessing the beautify URL.
location ~* signup/ {
rewrite ^/signup/([a-z0-9A-Z]+)/?$ /signup/?query=$1 break;
}
Please find the whole sample working config file given below with names example.com.conf in nginx/sites-enabled folder:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html/sites/example.com;
error_page 404 /error_404.html;
location = /error_404.html {
root /var/www/html;
internal;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
rewrite ^/abc1/signup/(.*)$ /abc1/signup/index.html?_l=$1 last;
rewrite ^/signup/(.*)$ /signup/index.html?_l=$1 last;
}
Thanks.
Related
I´m trying to serve a pdf file by making a configuration in nginx but I get the following error in the page: 404 Not Found
The configuration is like this:
server {
listen 3002;
index index.html;
server_name _;
location / {
root /var/www/html/pdf_carpet;
try_files $uri /index.html = 404;
}
}
pdf_carpet is where the pdf file is.
What could I do or change to be able to serve a pdf file in nginx?
P.S. It works with html files.
Here is the full location block that should show the PDF file in the browser window under the http://<your_domain_or_IP>:3002/pdf_carpet URL:
location = /pdf_carpet {
alias /var/www/html/pdf_carpet/file.pdf;
default_type application/pdf;
add_header Content-Disposition 'inline';
}
Update
If an URI for accessing the PDF file ends with the slash (or it is a root URI as a special case), the above config would not work since an index file name will be appended to such an URI by the nginx (making location = /path/ { ... } not match the $uri internal nginx variable). For such a case another technique can be used:
location = / {
root /var/www/html/pdf_carpet;
rewrite ^ /file.pdf break;
add_header Content-Disposition 'inline';
}
I'm trying to rewrite http://localhost/az to http://localhost/az.vcf (which exists as a file I want to be able to download.
I tried:
server {
server_name localhost;
root /www/data;
rewrite ^(/.*) $1.vcf last;
}
types {
text/x-vcard vcf;
}
I can download the file but it has no .vcf extension.
Rewrite mechanism works only with internal $uri variable. It does not affect URL in the browser address bar so it can't affect the downloaded file name. If you don't want to do a HTTP 301 redirect
rewrite ^(/.*(?<!\.vcf))$ $1.vcf permanent;
or HTTP 302 redrect
rewrite ^(/.*(?<!\.vcf))$ $1.vcf redirect;
you can try to add the Content-Disposition header to your response:
server {
server_name localhost;
root /www/data;
rewrite ^(?<path>.*/)(?<name>[^/]*) $path$name.vcf last;
add_header Content-Disposition 'attachment; filename="$name.vcf"';
}
I have a WordPress site that needed to 301 redirect old post to new post.
Old post:
https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/
New Post:
https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/
I added this rule in nginx.conf for this domain here
server
{
listen 111.222.333.444:80;
server_name example.com www.example.com ;
return 301 https://www.example.com$request_uri;
}
server
{
rewrite_log on;
rewrite ^/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/$ https://www.example.com/%e0%b8%a5%e0%b8%b2%e0%b8%81%e0%b9%88%e0%b8%ad%e0%b8%99/ permanent;
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
(the rest of location blocks continue)
}
Restart Nginx.
However, the old URL still return 404 and not a 301.
https://www.example.com/%e0%b8%aa%e0%b8%a7%e0%b8%b1%e0%b8%aa%e0%b8%94%e0%b8%b5/
And I don't see neither old nor new URI in error log at all. What should I do? Thanks!
The percent encoded URL is available in the $request_uri variable. But by the time Nginx is processing rewrite and location statements, the URL has been decoded and normalised.
Use a rewrite or location statement with the decoded values. For example:
rewrite ^/สวัสดี/$ /ลาก่อน/ permanent;
Or:
location = /สวัสดี/ {
return 301 /ลาก่อน/;
}
My forum is installed on the url: example.com/forums
I have used nginx and Vanilla to "prettify" the urls. I've set
/forum/conf/config.php, “RewriteUrls” to “True”.
and in my nginx.conf:
location /forums {
index index.php index.htm index.html;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 30d;
}
try_files $uri $uri/ #forums;
}
location #forums {
rewrite ^/forums(.+)$ /forums/index.php?p=$1 last;
}
The problem is I installed the sitemap plugin by Vanilla Forums.
and the resulting sitemap is supposed to be located at
example.com/forums/sitemapindex.xml
But when I navigate there nginx gives me a 404.
How do I solve this?
The problem is that the URI /forums/sitemapindex.xml is being processed by the location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ block and not being forwarded to /forums/index.php.
If you do not serve static .xml files, you could simply remove the |xml term from the regular expression.
Otherwise, you will need to make that URI a special case, for example:
location = /forums/sitemapindex.xml {
rewrite ^ /forums/index.php?p=/sitemapindex.xml last;
}
How can I exclude all URLs with a directory called dynamic in the following location block:
location ~* \.(?:js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
Here's the entire config, most of this comes from herokus php nginx buildpack
http://pastebin.com/xQ4BDtwr
( stackex won't let me post "mostly code" )
I would add the following location:
location /path/to/dynamic/ {
location ~* \.js$ {internal;}
}
The key is to override the ~* \.(?:js)$ regex location with a prefix location. Then you don't have to worry about where it appears in your config.
It could be solved with another regex location ~ /dynamic/.*\.js$ {internal;}, but then you would need to be sure it always comes before the ~* \.(?:js)$ location; another problem waiting to happen when your config grows.