nginx rewrite for new links structure - nginx

i just moved my server from apache to nginx, and my main site from joomla to wordpress (now it is a lot faster, but it cost me two months of learning nginx, and testing new configs etc.). Only problem that left is with new links structure.
Can somebody help me to rewrite old links to the new one?
This is mine links structure:
OLD> http://www.example.com/this-is-just-some-text-1234.html
NEW > https://www.example.com/this-is-just-some-text-09878
Main text in most cases stays the same, but post-id number is changed, and there is no .html at the end. http > https redirects are in nginx config already.

This can't really be done with nginx alone -- how would nginx have any idea what was the old ID with Joomla, and the new one with WordPress? It sounds like your transition process wasn't done correctly.
However, if you do have a list of old and corresponding new URLs, you can use the the map directive within nginx to supply such list.
map $uri $new_uri {
/this-is-just-some-text-1234.html /this-is-just-some-text-09878;
}
if ($new_uri) {
return 301 $new_uri;
}
References:
http://nginx.org/r/map
http://nginx.org/r/if

Related

Nginx rewrite / return unknown subfolders to specific directory

I am trying to achieve the following redirect. A migrated website had images orginally hosted in subfolders with (to the new site) unknown/random subfolders, like this:
/artikel_images/2938/30_01.jpg
/artikel_images/4050/20_11.jpg
/artikel_images/1291/image.jpg
On the new website all images are uploaded/hosted in:
/wp-content/uploads/2023/01/
Returning in the following result:
/wp-content/uploads/2023/01/30_01.jpg
/wp-content/uploads/2023/01/20_11.jpg
/wp-content/uploads/2023/01/image.jpg
I am trying to create a nginx redirect for all possible origin links (that are still in the content) to the single new link.
Hope to get some help, thanks!
I have been trying with several regular expressions, like below to achieve the redirect.
Edit: with thanks to Richard this is the solution:
location /artikel_images { rewrite ^/artikel_images/[0-9]+/(.+)$ /wp-content/uploads/2023/01/$1 last; }

Redirect a domain name to a specific page in NGINX and Next JS

I explain you my situation,
I have a website that we'll called website-a.com that is link to my personal project.
On this project, every users can have a page, with the url my-project.com/[username]. So the urls will then be my-project.com/user1, my-project.com/user2, ...
For the context, I'm building it in Next JS, with the following code architecture :
--
pages
-- [username]
-- index.tsx
-- about.tsx
--
And I'm using Nginx with pm2 and a proxy pass that points to my http://localhost:3000
Now, I'd like to redirect a second domain name to a specific page of a user.
So I would like that when I go to user1.com, it displays my-project.com/user1 without modifying the url.
I pointed the DNS of user1.com to my server, I created a new host nginx and I made a proxy_pass to http://localhost:3000/user1. It's working, but I don't have the ressources css and js, because next is trying to load them from user1.com and they don't exists (user1.com/css/abc.css, ...).
I also tried to modify the build-manifest.json in the .next folder created after the build and it didn't work
So either I would like to find a way for Next JS to take the resources from my-project.com even if it is on user1.com (let it be an absolute path), or put a configuration with the nginx host but I don't don't know which one.
Thanks you for your help

Nginx Config - Case Insensitive = 404

Newby at Nginx on Ubuntu. Main site is a WordPress site "example.com". No issue with this site. However, I need (cause someone did things in IIS and we need to move it to Nginx) to have a another site at "example.com/testsite" (not Wordpress, just static HTML).
Since this "testsite" was originally in IIS, sloppy coding was done and things are a complete mess of upper and lowercase.
So, I added a location block to my /ect/nginx/sites-enabled/example.com.conf
location /testsite {
alias /home/myusername/public_html/testsite;
index index.html;
}
This works! However as soon as a try to make this location case insensitve, I get a 404.
location ~* /testsite {
alias /home/myusername/public_html/testsite;
index index.html;
}
Ideas? Also, this site has tons of links that users can click on that are a mixture of upper/lower/proper and in no relation to the actual files that are on the server. Is there a way to make anything under that location block case insensitive (aka IIS way of doing it).
Thanks everyone.

Drupal doesn't generate thumbnails

I don't know why Drupal stopped generating thumbnails. So, I get error on files like this:
/sites/default/files/styles/choices/public/a1531172504.jpg?itok=Wn-VWDKd
Although the full image is found in:
/sites/default/files/a1531172504.jpg
I'm working on Nginx, Drupal 7
Last time I've answers question related to image derivatives I've got minuses so let be thorough here.
1.Drupal core will NOT create image derivative upon image upload - fact.
2.Thumbnail image (derivative from original image) is physically created upon HTTP request eg. if you visit the listing where image is used (when you visit the listing with thumbnail image the derivative is created on the fly via HTTP request)
3.To alter this behavior and to get a derivative image instantly upon image upload via CCK field you must use module Imageinfo Cache https://www.drupal.org/project/imageinfo_cache
With this info form above please recheck your site (go to listing where the thumbnail should appear and than check your thumbnail folder) If the image is still missing please provide more details like: Are you using CCK field or is it a custom field, are you creating derivative programmatically? Some code snippets are required here to solve you problem.
Also please check your .htaccess located in sites/default/files/.htaccess (not the general .htaccess)
and while you there check the permissions on files folder.
The solution for me was to add this lines to the nginx.conf files:
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
# not working with Drupal rewrite module: rewrite ^/?(.*?)/?$ /index.php?q=$1&$args;
rewrite ^/?(.*?)/?$ /index.php/$1&$args;
}
My environment is:
- docker-compose with nginx (nginx:1.17.4-alpine), drupal
(drupal:9.0.6-fpm-alpine) and mysql (mysql:8.0)
- drupal (9.0.6), nginx/1.17.4, PHP 7.4.10, MySql 8.0.21
The server responded with a 404 error because:
The image styles are generated when drupal receives a request.
Nginx receives a request for an image that is not in the filesystem; then returns a 404 instead redirect the request to drupal.
The rule I propose for the nginx.conf redirect the image request to drupal and solve the problem after restart the web server:
docker-compose restart webserver
My fix was to disable and re-enable the clean-url. .../admin/config/search/clean-urls".

Nginx Rewrite Rules - All Sub Folders

I am migrating site and bringing all my product urls to top level.
Example:
Old URL: http://www.example.com/sub/folder/product.asp
Old URL: http://www.example.com/sub1/folder1/product.asp
New URL: http://www.example.com/product.asp
I am struggling to get the nginx rewrite rule to work how i need it. Basically anything that ends in *.asp need to strip out all the folder paths.
You need to identify URIs ending with .asp that include more than one /. One possible solution might be:
rewrite ^/.+(/[^/]+\.asp)$ $1 permanent;
See this document for details.

Resources