My setup looks as following:
example.com/de is now example.de, example.com/en is now example.com. I'm using nginx latest version.
I need to preserve the post that is sent via iOS app to http://www.example.com/de/api/uploadPicture, which is now http://www.example.com/api/uploadPicture.
I found out that I need to use location instead of rewrite rule.
This is what I have in the .com block:
location /de/shop {
rewrite ^/de/shop/(.*) http://www.example.de/$1 redirect;
}
location /de {
rewrite ^/de/(.*) http://www.example.de/$1 redirect;
}
location /en/shop {
rewrite ^/en/shop/(.*) http://www.example.com/$1 redirect;
}
location /en {
rewrite ^/en/(.*) http://www.example.com/$1 redirect;
}
This is the mentioned part:
location /de/api/uploadPicture {
# Not sure how to use this one
#proxy_pass http://myproject$uri$is_args$args;
#proxy_redirect http://localhost:8080//;
# This works, but looses the post data
rewrite ^/de/api/(.*) http://www.example.com/api/$1 redirect;
}
Thanks for your appreciated help.
EDIT: I've adjusted with the given solution. This works, but not in combination with the rewrite rules:
server {
client_max_body_size 20M;
listen 80;
listen 443 ssl;
ssl_certificate /home/ib/ssl/ib.pem;
ssl_certificate_key /home/ib/ssl/ib.key;
server_name www.example.com;
location / {
proxy_pass http://myproject;
error_page 500 502 503 504 /error.html;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-REAL-SCHEME $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /error.html {
root /home/ib/error;
}
location ~ ^/de/api/(?<method>.*)$ {
if ($request_method = POST) {
return 307 http://www.example.de/api/$method$is_args$args;
}
# non-POST requests
rewrite ^/de/api/(.*) http://www.example.com/api/$1 redirect;
}
location ~ ^/en/api/(?<method>.*)$ {
if ($request_method = POST) {
return 307 http://www.example.com/api/$method$is_args$args;
}
# non-POST requests
rewrite ^/de/api/(.*) http://www.example.com/api/$1 redirect;
}
rewrite ^/de/shop/(\d+)/(.*) http://www.example.de/$1/$2 permanent;
rewrite ^/en/shop/(\d+)/(.*) http://www.example.com/$1/$2 permanent;
rewrite ^/de/(.*)-(\d+) http://www.example.de/$1-$2.html permanent;
rewrite ^/en/(.*)-(\d+) http://www.example.com/$1-$2.html permanent;
rewrite ^/de/shop/(.*)-(\d+) http://www.example.de/$1-$2.html permanent;
rewrite ^/en/shop/(.*)-(\d+) http://www.example.com/$1-$2.html permanent;
rewrite ^/de(.*)$ http://www.example.de$1 permanent;
rewrite ^/en(.*)$ http://www.example.com$1 permanent;
Using 307 redirects for POST requests will allow you to keep the POST data. In your case something like this should work fine:
location ~ ^/de/api/(?<method>.*)$ {
if ($request_method = POST) {
return 307 http://www.example.com/api/$method$is_args$args;
}
# You can keep this for non-POST requests
rewrite ^/de/api/(.*) http://www.example.com/api/$1 redirect;
}
Related
I used this Nginx configuration and everything went well.
server {
listen 80;
server_name 127.0.0.1;
ssl_client_certificate /etc/ssl/certs/server.crt;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options "nosniff";
location ^~ /api/ {
rewrite /(.*) /proxy/$1;
}
location ^~ /js/ {
rewrite /(.*) /proxy/$1;
}
location ^~ /services/ {
rewrite /(.*) /proxy/$1;
}
location ^~ /payeewebv2/ {
rewrite /(.*) /proxy/$1;
}
location /proxy/ {
proxy_set_header X-SSL-CERT $ssl_client_escaped_cert;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://proxieddomain.com/;
proxy_ssl_server_name on;
proxy_set_header host $proxy_host;
}
}
Now I need to proxy to be determined by a COOKIE. I have updated the configuration and it looks like this:
server {
listen 80;
server_name 127.0.0.1;
ssl_client_certificate /etc/ssl/certs/server.crt;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options "nosniff";
location ^~ /api/ {
rewrite /(.*) /proxy/$1;
}
location ^~ /js/ {
rewrite /(.*) /proxy/$1;
}
location ^~ /services/ {
rewrite /(.*) /proxy/$1;
}
location ^~ /payeewebv2/ {
rewrite /(.*) /proxy/$1;
}
location /proxy/ {
resolver 127.0.0.11;
set $proxy "to_be_replaced";
proxy_set_header X-SSL-CERT $ssl_client_escaped_cert;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
set_by_lua_block $proxy {
return "https://proxieddomain.com/"
}
proxy_pass $proxy$request_uri;
proxy_ssl_server_name on;
proxy_set_header proxy $proxy$request_uri;
proxy_set_header host $proxy_host;
}
}
}
This is the changed part:
set_by_lua_block $proxy {
return "https://proxieddomain.com/"
}
proxy_pass $proxy$request_uri;
I can visit http://localhost/varicent/ and it proxies to https://proxieddomain.com/
But when its javascript requests /api/something etc. it returns content from $proxy (https://proxieddomain.com/) and not from $proxy/api/something (https://proxieddomain.com/api/something).
Do you know why the first non-dynamic solution works without problems?
What should i change to make it work it like before?
Thx
The problem has been in really the domain.
I looked it up through nslookup and changed configuration to use canonical name which is different from the https://proxieddommain.com.
Thank you #IvanShatsky for your tip.
I'm trying to migrate from WordPress to self hosted Ghost blog. In the process I wish to clean up url's for my posts from https://example.com/categoryid/slug to https://example.com/slug. Cateogryid is seems to be whole number containing 1-4 digits.
The problem is that I have also urls that I don't want to rewrite
don't rewrite for img: https://example.com/content/images/2020/01/logo.png
rewrite for post: https://example.com/1886/slug
What I have tried:
this works, but for both url's
rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
this should be a match with an online regEx tester, but does not work
rewrite \.*(com)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
passing proxy before and after rewrite rules
location /content/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass example_ip ;
}
full config:
server {
listen 80;
listen [::]:80;
server_name www.example.com example.com;
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
# redirect /id/slug -> slug
rewrite ^(.*)(\/\d*\/)(.*)$ https://example.com/$3 redirect;
# redirect category -> tag
rewrite (category\/)(.*)$ https://example.com/tag/$2 permanent;
# redirect blog -> archive
rewrite (blog\/)$ https://example.com/archive permanent;
root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/nginx/snippets/ssl-params.conf;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://example_ip;
}
location ~ /.well-known {
allow all;
}
client_max_body_size 50m;
}
In the URLs https://example.com/content/images/2020/01/logo.png and https://example.com/1886/slug, the URI seen by the rewrite directive is /content/images/2020/01/logo.png and /1886/slug respectively.
You need to rewrite URIs which contain 1 to 4 digits in the first path element.
Use either:
rewrite ^/\d+(/.*)$ $1 redirect;
Or:
rewrite "^/\d{1,4}(/.*)$" $1 redirect;
The last variant must use the quotes to protect the embedded brace characters.
See this document for details.
I want to redirect all traffic to HTTPS, except two locations for domain validation.
Here is my config:
server {
server_name xxxx.de www.xxxx.de;
# SSLMate domain validation
location /.well-known/pki-validation/ {
proxy_pass https://xxxx.http-approval.sslmate.com$request_uri;
proxy_set_header X-Forwarded-Host $http_host;
resolver 8.8.8.8;
}
location /.well-known/acme-challenge/ {
proxy_pass https://xxxx.http-approval.sslmate.com$request_uri;
proxy_set_header X-Forwarded-Host $http_host;
resolver 8.8.8.8;
}
# Redirect everything else to HTTPS
location / {
rewrite ^ https://xxxx.de/index.htm permanent;
}
}
Unfortunately everything is picked up by the rule for the "/" location. What am I doing wrong?
I have a URL as
https://test.rockon.me/Profiles/XYZ-ABC-PQRS/default.aspx
now using nginx i have to write rules for creating a subdomain which can make the URL as https://XYZ-ABC-PQRS/test.rockon.me/Profiles/default.aspx here XYZ-ABC-PQRS is the username of some user.
server
{
access_log /var/log/nginx/subcalls.log;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Access-Control-Allow-Origin *;
listen 80;
server_name ~^(?<subdomain>.+)\nithinveer\.com$;
location /
{
proxy_pass http://192.168.6.190/Profiles/$subdomain$request_uri/;
}
Yours not working, probably because you expect $request_uri = /default.aspx which is not, it is actually, everything after subdomain i.e., /Profiles/user/default.aspx
Try this,
server {
server_name test.rockon.me;
rewrite ^/Profiles/(.*)/default.aspx http://$1/test.rockon.me/Profiles/default.aspx permanent;
}
The other option using proxy_pass can be,
server{
server_name test.rockon.me;
location / {
rewrite /Profiles/(.*)/(.*) /Profiles/$1/$2 break;
proxy_pass http://192.168.6.190;
}
}
Hope it helps. :)
I have moved my site to a new server running nginx from apache.
The old site structure was:
example.com/NEW/
The new structure:
example.com/
I have a few applications that still reference uploads/files like:
example.com/NEW/assets/images/7871862618261826.jpg
I need to rewrite all the requests that include the /NEW/ parameter.
eg:
example.com/NEW/assets/images/7871862618261826.jpg
should be routed to:
example.com/assets/images/7871862618261826.jpg
My Current Nginx config looks like:
server {
listen 80;
server_name www.domain.com domain.com;
return 301 https://www.domain.com$request_uri;
}
server {
listen 443;
if ($http_host = domain.com) {
rewrite (.*) https://www.domain.com$1;
}
location /NEW/ {
rewrite ^/NEW(.*)$ $1 last;
#return 405;
#return 301 https://www.domain.com$request_uri;
}
server_name domain.com www.domain.com;
access_log /var/log/nginx/domaincom.access.log;
error_log /var/log/nginx/domaincom.error.log;
root /var/www/domain.com/public_html/;
index index.html index.htm index.php;
#set default location
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}
How can I achieve the rewrite?
I Finally got it working with the below location block:
location ~ /NEW/.*\.(png|jpg|gif|pdf|doc)$ {
rewrite ^/NEW(.*)$ $1 last;
}
It seems the location block was not being executed as required.