Hi need to create rewrite:
Expected results:
FOR REQUEST: sub4.sub3.example.com
LOAD: sub4.example.com/sub3
I tried this:
server{
listen 80 default_server;
server_name ~^(?<subdomain>\w+)\.test\.server$;
location / {
rewrite ^ http://test.server/$subdomain/$request_uri permanent;
}
}
But it not work as suspect with recursive redirects.
Any work around?
UPD:
server{
listen 80;
server_name domain.test.server test.server;
root /usr/share/nginx/html;
location / {
if ($host ~* ^(?<sub>.+)*\.test\.server$ )
{ rewrite ^/(.*)$ http://test.server/$sub/$1; }
}
}
}
THIS WORKS BUT WITH REDIRECT:
curl -I http://domain.test.server/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.12.2
Date: Fri, 20 Sep 2019 10:17:52 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://test.server/domain/
Is there some way without redirect?
server{
listen 80;
server_name ~^(?<lev4>.+)*\.(?<lev3>.+)*\.(?<lev2>.+)*\.(?<lev1>.+)*$ ;
root /var/www/app;
location / {
proxy_set_header Host $lev4.$lev2.$lev1 ;
proxy_pass http://127.0.0.1:80;
if ($host ~* ^(?<lev4>.+)*\.(?<lev3>.+)*\.(?<lev2>.+)*\.(?<lev1>.+)*$ ) {
rewrite ^/(.*)$ /$lev3/$1 break;
}
}
}
server{
listen 127.0.0.1:80;
server_name sub4.example.com;
root /var/www/app;
location / {
}
}
Related
I am struggling to implement an automatic nginx redirect from non index pages to my index page, with the exception of /admin
For instance, example.com/test should redirect to example.com, but example.com/admin should not redirect to example.com
This is my current nginx configuration file:
upstream app_server {
server unix:/tmp/mysite.sock;
}
proxy_cache_path /var/www/example.com/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name www.example.com example.com;
# redirects both www and non-www to https
return 301 https://www.example.com$request_uri;
}
server {
listen 443;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/example.com/media;
}
location /static {
alias /var/www/example.com/static;
}
location / {
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
proxy_ssl_server_name on;
}
}
I have tried adding a try_files statetement within my location / block, and other things, but none seem to work. Am I missing something?
You are trying to mix proxy_pass with try_files, it won't work within the same location block. You can use named location instead and rewrite any URI that doesn't start with /admin to a root one using negative regex assertion:
location / {
try_files $uri #app;
}
location #app {
rewrite ^(?!/admin) / break;
proxy_cache my_cache;
include proxy_params;
proxy_pass http://app_server;
}
You don't need the separate location /media { ... } or location /static { ... } blocks, because as nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
Instead you just need to define the common server root (outside of any location blocks):
root /var/www/example.com;
You are also don't need to use the proxy_ssl_server_name directive since you are not proxying your request to the upstream with HTTPS protocol.
I want to pass requests like this:
http://mydomain.io --> http://127.0.0.1:8080
http://mydomain.io/aa/bb --> http://127.0.0.1:8080/aa/bb
http://api.mydomain.io --> http://127.0.0.1:10000
http://api.mydomain.io/cc/dd --> http://127.0.0.1:10000/cc/dd
and my /etc/nginx/conf.d/default.conf file:
listen 80;
server_name mydomain.io;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
server {
listen 80;
server_name api.mydomain.io;
location ~ ^/(.*)$ {
proxy_pass http://127.0.0.1:10000/$1;
}
}
When I send request to http://api.mydomain.io:10000/xxx, it works,
but http://api.mydomain.io/xxx doesn't work.
(It returns 502 Bad Gateway Error)
What is the problem with my config file?
default.conf
server { # This one before the wilcard domain
listen 80;
server_name api.mydomain.io;
location / {
proxy_pass http://127.0.0.1:10000;
}
}
server { # always at the end
listen 80 default_server;
server_name *.mydomain.io;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
I have this code:
server {
listen 80;
server_name example.com;
return 301 https://www.sajufortune.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
set $alb internal-saju-alb-before-w43.us-west-2.elb.amazonaws.com;
location / {
proxy_pass $alb;
}
}
server {
listen 80;
location /ping/ {
return 200 'pong';
}
}
I got this returning 200 code from here:
I want to 200 response pong to /ping/ request from any url except example.com, www.example.com.
How can I do this?
Try the below config
server {
listen 80;
server_name example.com;
return 301 https://www.sajufortune.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
if ($http_x_forwarded_proto != 'https') {
rewrite ^ https://$host$request_uri? permanent;
}
set $alb internal-saju-alb-before-w43.us-west-2.elb.amazonaws.com;
location / {
proxy_pass $alb;
}
}
server {
listen 80;
server_name _;
location /ping/ {
return 200 'pong';
}
}
According to http://nginx.org/en/docs/http/ngx_http_core_module.html#listen you need to either put the ping/pong server-block on the top, or append default_server on the listen option (e.g. listen 80 default_server;)
I have 3 NGINX hosts, that I only want to serve on HTTPS. Two of them work correctly, however, one of them resolves the wrong host. Here's all of the info
Nginx virtual hosts
# cat alpha.domain-a.tld
server {
listen 80;
server_name alpha.domain-a.tld;
return 301 https://alpha.domain-a.tld$request_uri;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/nginx/certs/alpha.domain-a.tld.pem;
ssl_certificate_key /etc/nginx/certs/alpha.domain-a.tld.key;
ssl_client_certificate /etc/nginx/certs/cloudflare.crt;
ssl_verify_client on;
root /var/www/alpha.domain-a.tld/;
index index.html;
server_name alpha.domain-a.tld;
location / {
try_files $uri $uri/ $uri.html =404;
}
}
# cat mike.domain-a.tld
server {
listen 80;
server_name mike.domain-a.tld;
return 301 https://mike.domain-a.tld$request_uri;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/nginx/certs/domain-a.tld.pem;
ssl_certificate_key /etc/nginx/certs/domain-a.tld.key;
ssl_client_certificate /etc/nginx/certs/cloudflare.crt;
ssl_verify_client on;
root /var/www/mike.domain-a.tld/;
index index.html;
server_name mike.domain-a.tld;
location / {
try_files $uri $uri/ $uri.html =404;
}
}
# cat juliet.domain-b.tld
server {
listen 80;
server_name juliet.dommain-b.tld;
return 301 https://juliet.domain-b.tld$request_uri;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/nginx/certs/domain-b.tld.pem;
ssl_certificate_key /etc/nginx/certs/domain-b.tld.key;
ssl_client_certificate /etc/nginx/certs/cloudflare.crt;
ssl_verify_client on;
root /var/www/juliet.domain-b.tld;
index index.html;
server_name juliet.domain-b.tld;
location / {
try_files $uri $uri/ $uri.html =404;
}
}
Alpha and mike resolve correctly, however, when i try to access http://juliet, it redirects me to alpha rather than https://juliet, as shown below:
# curl -I --resolve alpha.domain-a.tld:80:127.0.0.1 http://alpha.domain-a.tld/
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: #OMMITED
Content-Type: text/html
Content-Length: #OMMITED
Connection: keep-alive
Location: https://alpha.domain-a.tld/
# curl -I --resolve mike.domain-a.tld:80:127.0.0.1 http://mike.domain-a.tld/
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: #OMMITED
Content-Type: text/html
Content-Length: #OMMITED
Connection: keep-alive
Location: https://mike.domain-a.tld/
# curl -I --resolve juliet.domain-b.tld:80:127.0.0.1 http://juliet.domain-b.tld/
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: #OMMITED
Content-Type: text/html
Content-Length: #OMMITED
Connection: keep-alive
Location: https://alpha.domain-a.tld/
Could anyone help me find out why juliet is resolving the first alphanumeric host (alpha) rather than juliet?
Look at the server_name of juliet:
# cat juliet.domain-b.tld
server {
listen 80;
server_name juliet.dommain-b.tld;
return 301 https://juliet.dommain-b.tld$request_uri;
}
juliet.dommain-b.tld probably doesn't exists? I think your curl-command is correct (with the correct url) but in your nginx config you wrote the wrong name. Your nginx server doesn't know the domain but the dns request resolves correctly to your server and so your server returns the first entry of your nginx config.
Probably yet another nginx redirect question... couldn't find the answer, so:
How do I redirect http://domain.com to http://www.domain.com but do not rewrite any subdomain http://*.domain.com given the cloudfoundry nginx configuration which contains this server definition:
server {
listen 80;
server_name _;
server_name_in_redirect off;
}
I tried this configuration
server {
server_name domain.com
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
listen 80;
server_name _;
server_name_in_redirect off;
}
but am getting infinite redirects.
Try replacing
server_name _;
with
server_name *.domain.com;
server {
listen 80;
server_name domain.com
return 301 http://www.domain.com$request_uri;
}
server {
listen 80 default_server;
...
}
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/en/docs/http/converting_rewrite_rules.html