Nginx rewrite a URL to another format - nginx

I want to rewrite a URL to another URL format in NGINX configuration. Here is the configuration:-
server {
server_name example.com;
location / {
proxy_pass http://localhost:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static/ {
alias /var/www/test/;
rewrite ^/static/.*/(.*)/report/(.*)$ /static_contents/$1/img/$2 break;
}}
Here URL would be
http://example.com/static/download/some_name/report/image/3.jpeg
i want rewrite rule to convert above url to this format
http://example.com/static_contents/some_name/img/3.jpeg
Content location is
/var/www/test/static_contents/some_name/img
Unable to figure out the way to do this.
When i try to log i get errors as
"alias" cannot be used in location "/static/" where URI was
rewritten, client:

Related

Nginx: create a location to redirect to another location

I'm trying to redirect a location to another location, but for some reason it doesn't work. My nginx service is running under a Docker container on 8080:80 port.
I have /portal that shows my web application correctly and I would like nginx redirects from / to /portal. I've tried some approaches using location / or location = /, but with no success.
The /portal can be accessed normally by
http://localhost:8080/portal
but for some reason when I access
http://localhost:8080/
the browser redirects to
https://localhost/portal
instead of redirecting to:
http://localhost:8080/portal
Locations:
upstream webportal {
server portal_container:8080;
}
location / {
return 301 /portal;
}
location /portal {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://webportal;
}
I was able to fix the problem by adding absolute_redirect off; to my location:
location / {
absolute_redirect off;
return 301 /portal;
}

Problem with nginx location and proxy_pass

I have a rule in my nginx.conf that does not work and I have no idea why. According to the documentation it should work. Part of the config looks like this.
The first rule on port 8100 works and redirects the call http://example.com/api/domains to https://localhost:8181/oan/resources/domains
# Working
server {
listen 8100 default_server;
server_name example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
root /var/www/html/example;
location /api {
proxy_pass https://localhost:8181/oan/resources; break;
}
# For ReactJS to handle routes
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ / break;
}
}
}
# Not working
server {
listen 8200;
server_name api.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
location / {
proxy_pass https://localhost:8181/oan/resources; break;
}
}
The last call to port 8200: http://api.example.com:8200/domains SHOULD redirect to: https://localhost:8181/oan/resources/domains but does NOT do that.
What is wrong with this config and how can I get the last rule on port 8200 do the correct stuff, always redirect to https://localhost:8181/oan/resources/$uri
When you use proxy_pass with an optional URI within a prefix location block, Nginx will transform the requested URI by performing a straight text substitution.
In your case, the prefix location value is / and the optional URI value is /oan/resources. So a requested URI of /foo will be transformed into /oan/resourcesfoo.
For correct operation, both values should end with / or neither end with /.
For example:
location / {
proxy_pass https://localhost:8181/oan/resources/;
}
See this document for details.

how to set the locaction in nginx.conf for removing URL's trailing slash?

Today I used two servers for nginx, the content of nginx.conf as follows:
#192.168.2.98
server {
listen 8091;
location ^~ /ttank {
alias /develop/servers-running/front/vue-public/dist;
index index.html;
try_files $uri $uri/ /ttank/index.html;
}
}
#192.168.2.97
location /ttank {
proxy_pass http://192.168.2.98:8091;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
I can access the 192.168.2.98:8091/ttank by enter address: http://192.168.2.98:8091/ttank in chrome, I also can access 192.168.2.98's ttank by entering the address http://192.168.2.97/ttank/, but when I change the addres http://192.168.2.97/ttank/ into http://192.168.2.97/ttank, my chrome entered into waiting status forever, the only difference between two addresses is the last "/", I don't know how to modify the config file for removing the last "/" when accessing ttank by 192.168.2.97?
Try usinge a rewrite rule to get rid of the ending slashes
location /ttank {
rewrite ^/(.*)/$ /$1 break;
...;
...;
proxy_pass ...;
}
it should do it

Nginx lost my static images for one site in sites-enabled

I have two sites in sites-enabled for nginx:
1) project - this is essentially the top level domain - mysite.com
server {
listen 80;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/project/static/;
}
}
2) blog - this is for a blog, that is accessible via: mysite.com:8080
server {
listen 8080;
location blog/ {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/flask-deploy/blog/static/;
}
}
Nginx has accepted both configurations, but when I visit 1 i get all of the text, but no css, or images.
If I visit 2 i get everything working fine.
What am I doing wrong?
Thank you
For the alias directive to work correctly, the URI in the location and the URI in the alias directive should both end in / or neither end in /. The algorithm seems to substitute one string for the other.
So you should probably write:
location /static {
alias /home/www/flask-deploy/project/static;
}
However, when the last element(s) of the alias match the location, the root directive is preferred (see this):
location /static {
root /home/www/flask-deploy/project;
}
Your location blog/ looks incorrect, prefix locations should always begin with a /.

Nginx redirect with rewrite

Assuming I want to redirect URIs like
http://server1:8081/test/admin/option?options
http://server1:8081/test/admin/option/suboption?options
http://server1:8081/test/admin/option/suboption/subsuboption?options
to
http://server2:8080/tomcat/admin/option?options
http://server2:8080/tomcat/admin/option/suboption?options
http://server2:8080/tomcat/admin/option/suboption/subsuboption?options
what nginx rules I have to use? I've tried the following but it doesn't work
location =/test/admin {
proxy_pass http://server2:8080/tomcat/admin;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Try something along these lines ..
location /test/admin {
rewrite ^/test/admin(.*)$ /tomcat/admin$1;
}
location /tomcat/admin {
internal;
proxy_pass http://server2:8080;
[…]
}
That is, rewrite the requests to "tomcat/admin" which you can optionally make open to internal requests only.
In that location block, you can then proxy passthe request.

Resources