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.
Related
I have nginx configuration file which has to server example.com and www.example.com.
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
auth_basic "example Login";
auth_basic_user_file /etc/nginx/.htpasswd;
root /projects/www/example;
index index.html;
location ~* \.(html|js|jpg|png|gif|css|perfumes|imgs|map|fonts|otf)$ {
index index.do index.html index.htm;
access_log off;
}
location /.protected {
access_log off;
auth_basic off;
}
location /health {
access_log off;
auth_basic off;
}
location / {
try_files $uri $uri/index.html;
}
location /hello {
proxy_pass http://localhost:8282;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
I am trying to protect this domain for now, cuz we are preparing the launch. But there is only link which should let the requests pass through without password, if a link has '.protected' in it.
For example,
www.example.com/.protected/file1.txt should be allowed without entering a password.
It is working ok, but the issue is example.com/.protected/file1.txt (without 'www').
If I type only domain name 'example.com' (without 'www'), it automatically redirects to www.example.com as configured, but 'example.com/.protected/file1.txt' doesn't redirect to 'www.example.com/.protected/file1.txt'. It seems if the domain name has some paths, the domain name (without 'www') doesn't redirect to 'www.example.com'
I am getting 'curl: (6) Could not resolve host:'
Is there anything wrong with my configuration file?
I need to:
point site.com to my site.github.io (that works)
point site.com/anything to filesystem /site/dist (that works)
point site2.com to site.com to filesystem /root/dist (that doesn't work, here it shows site.github.io)
site.com.conf
server {
listen 80;
server_name site.com;
location = / {
proxy_pass http://site.github.io;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
root /site/dist;
try_files $uri /index.html;
}
}
site2.com.conf
server {
listen 80;
server_name site2.com;
proxy_set_header Host site.com;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://127.0.0.1/$request_uri;
}
}
In the case http://site2.com you're going to end up with uri = "/" when hitting site.com, which you have set to proxy to site.github.io.
You may want to do instead for site2 is:
location / {
proxy_pass http://127.0.0.1/site2/$request_uri;
}
Then in site.com:
location ~ ^/site2(.*)$ {
root /site/dist;
try_files $1 /index.html;
}
OR
location /site2 {
rewrite ^/site2(.*)$ $1 break;
root /site/dist;
try_files $uri /index.html;
}
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;
}
I want to strip the www from my url in my nginx configuration and looking around the documentation and stack overflow posts I wrote the following configuration but it doesn't seem to be working.
server_name {
server_name www.subdomain.domain.com;
rewrite ^(.*) https://subdomain.domain.com/$1 permanent
}
server_name {
server_name subdomain.domain.com;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /file;
index index.html index.htm app.js;
location /{
proxy_pass https://subdomain.domain.com:443/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /*/ {
proxy_pass https://subdomain.domain.com:443/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
error_page 404 /404.html;
}
But it doesn't seem to be working at all. However whenever I try to go to the site using www.subdomain.domain.com i can't access the site but doing https://subdomain.domain.com works fine. Any advice on this would be great thanks.
You have typo in nginx config (server_name instead server), and try return instead rewrite as more proper way:
server {
server_name www.subdomain.domain.com;
return 301 $scheme://subdomain.domain.com$request_uri;
}
I have the following nginx configuration
rewrite_log on;
server {
server_name greymarmita.no-ip.org;
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
server {
server_name greymarmita.no-ip.org;
listen 443 ssl;
error_log /var/log/nginx/main.error;
access_log /var/log/nginx/main.access;
ssl on;
ssl_certificate /etc/ssl/localcerts/autosigned.crt;
ssl_certificate_key /etc/ssl/localcerts/autosigned.key;
root /srv/www;
index index.html /index.html;
location /rasp/ {
proxy_pass http://192.168.2.6:81/;
}
location /cam/ {
proxy_pass http://192.168.2.4:8081;
}
location ^~ /router/ {
proxy_pass http://192.168.2.1/;
}
location /nas/ {
proxy_pass http://192.168.2.13/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
However when I try to access http://192.168.2.6/nas although the html files are served correctly, files under /web/ are not
GET https://greymarmita.no-ip.org/web/images/login.png 404 (Not Found)
The correct path for these assets should be https://greymarmita.no-ip.org/nas/web/images/login.png
You don't have a location to match that file, try adding this
location / {
try_files $uri $uri/;
}