I have a small VueJS app which is located in index.html. For SEO value I've also started generating a lot of static html files in subfolders. The problem is that Nginx only routes my requests to index.html, and not to these static html files.
I'd like all requests to for instance mydomain.com/podcast/test-podcast/first-episode.html to serve /podcast/test-podcast/first-episode.html
What directive do I need to add in Nginx to add such a rule?
Here's my conf file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/nginx/html;
index index.html;
server_name localhost;
try_files $uri $uri/ /index.html?q=$uri&$args;
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
Long work but not impossible; For each page you will need to create a NGinx rules
...
location /podcast/test-podcast/first-episode.html {
root /usr/share/nginx/html/podcast/test-podcast/
index first-episode.html
}
location /blabla/seo-roxx/example.html {
root /blabla/seo-roxx/example/
index example.html
}
...
You may want to rethink you SEO strategie to avoid doing it in NGinx.
Related
I'm trying to serve WordPress from the subdirectory site.com/blog but im facing 404, tried different configurations but none of them are working getting a 404.
The WordPress files are at /home/ubuntu/wordpress
Here is my Nginx configuration, I'm serving static files for the root domain site.com and running Laravel for backend API, I think it conflicts with the FastCGI settings, not sure though.
server {
root /home/ubuntu/mysite/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name mysite.com www.mysite.com;
access_log off;
include /etc/nginx/badbots.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|json|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
expires 365d;
add_header Cache-Control "public, max-age:24h";
}
large_client_header_buffers 4 32k;
location /blog {
alias /home/ubuntu/wordpress;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
listen 80;
}
With my current configuration the following URL triggers redirects which are really bad for SEO.
http://localhost/blog redirects to -> http://localhost/blog/
Here is my conf.d/default.conf file
server {
listen 80 default_server;
server_name localhost;
server_name_in_redirect on;
location / {
add_header Cache-Control "public";
expires 1y;
root /work/front-page/dist/browser;
}
}
I would like both location to give status 200, or at least to reverse the redirect
http://localhost/blog/ redirects to -> http://localhost/blog
EDIT:
putting
try_files $uri $uri/index.html =404;
seems to resolve the issue, thanks #Richard Smith
Here is the full working configuration, I tested it with http -> https redirect too.
server {
listen 80 default_server;
server_name localhost;
server_name_in_redirect on;
location / {
add_header Cache-Control "public";
expires 1y;
try_files $uri $uri/index.html =404;
root /work/front-page/dist/browser;
}
}
In the root directory, any redirect to localhost/ is simply redirected to index.php, but if you go inside the localhost/phone subdirectory, there's an index.php page too, and as you type localhost/phone/ you are not redirected to the index.php page. No idea why. This is my server configuration.
server {
listen 80;
listen [::]:80 default_server;
server_name localhost;
charset UTF-8;
index index.html index.php;
location / {
root xyz;
proxy_set_header Connection "";
if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
try_files $uri $uri/ $uri.html $uri.php?$args;
rewrite ^/(.*)/$ /$1 permanent;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|webp)$ {
expires 365d;
}
}
I tried to point the directory and added defaults for index.php and it works, but now all the url's inside /phone/ are showing blank.
location /phone {
index index.html index.php;
}
By "showing blank" means that they show a 404 error according to the error log
2019/05/08 23:40:58 [error] 26240#14076: *1 CreateFile()
"C:\xyz\nginx/xyz/phone/index" failed (2: The system cannot find
the file specified), client: ::1, server: localhost, request: "GET
/phone/index HTTP/1.1", host: "localhost"
Now the index.php shows a 404 error, what am I doing wrong? Even though the file is there
If you want to avoid a trailing / for all URIs, but still test for index.html and index.php, you should avoid using the index directive and the $uri/ term with the try_files directive.
One approach is to use a named location to test for PHP files. This location will need your fastcgi configuration.
For example:
root /path/to/root;
location / {
if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
rewrite ^/(.*)/$ /$1 permanent;
try_files $uri $uri.html $uri/index.html #php;
}
location #php {
try_files $uri.php $uri/index.php =404;
fastcgi_pass ...;
...
}
I'm trying to make a cdn for fonts. But it's giving cross-origin error for fonts. Though I am allowing cross-origin.
Here is my nginx config file
server {
listen 80;
root /var/www/cdn.example.com/public_html;
index index.html index.htm index.php;
server_name cdn.example.com www.cdn.example.com;
location / {
add_header Access-Control-Allow-Origin *;
try_files $uri $uri/ /404.html;
}
location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf|ttf|ttc|otf|eot|woff|woff2)$ {
add_header Access-Control-Allow-Origin "*";
expires max;
}
}
Fonts files are in /var/www/cdn.example.com/public_html/fonts
The issue has been solved. It just needs some time to take effect the cross-origin.
I'm trying to upload a bunch of html and image files to my Nginx webserver which is running Ghost (the blogging platform) lets call it ghost-blog.com. Ghost runs perfectly fine, but additionaly I want serve other files and folders under the same domain e.g. ghost-blog.com/text.html and ghost-blog.com/subfolder/index.html.
After spending some time googling for an answer it seems I've bumped into something "new". I am aware I need to make changes to the /etc/nginx/sites-available/default file. What I don't know is what to add/edit so that
I create a /some/random/public folder public
This does not conflict with Ghost which is already serving content, specially the default index index.html index.htm files.
My current /etc/nginx/sites-available/default config file looks like this:
server {
listen 80;
server_name www.ghost-blog.com;
rewrite ^/(.*) http://ghost-blog.com/$1 permanent;
}
server {
root /usr/share/nginx/www;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
}
Any suggestions on how I could go around creating a /public folder serving other files and sub-folders?
server {
listen 80;
server_name www.ghost-blog.com/subfolder;
rewrite ^/(.*) http://ghost-blog.com/subfolder/$1 permanent;
}
server {
root /usr/share/nginx/www/NEWSITEFOLDER;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
}
then in ssh you need to make this directory to run the new ghost blog from
/usr/share/nginx/www/NEWSITEFOLDER;
so run command
mkdir /usr/share/nginx/www/NEWSITEFOLDER;