I trying to get custom url from memcached server, assign it to variable using eval module and tell nginx to rewrite request.
My config file is included in http section of default nginx.conf and has the following code:
map $request_uri $instanceId {
"~/instance1" "instance1"; #"http://127.0.0.1:8088";
"~/instance2" "instance2"; #"http://127.0.0.1:8089";
}
log_format myVariable '{"evaluation_response" : "$proxyUrl", "for instance" : "$instanceId"}';
log_format redirectUrl '{"redirect" : "$proxyUrl/$relative_request_uri?$query_string"}';
server {
listen 8058;
server_name "memcahed.test.com";
location ~(?:\/instance\d)?\/(?<relative_request_uri>(?:\w*\W*)*){
eval_override_content_type text/plain;
eval_escalate on;
eval $proxyUrl {
if ($instanceId = '') {
return 502;
}
set $memcached_key $instanceId;
memcached_pass 127.0.0.1:11211;
}
#access_log /home/Vlad/myAccess.log myVariable;
proxy_pass "$proxyUrl/$relative_request_uri?$query_string";
#access_log /home/Vlad/myAccess.log redirectUrl;
}
}
server {
listen 8088;
listen [::]:8088;
server_name "i1.test.com";
root /var/www/test.com/html;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
}
server {
listen 8089;
listen [::]:8089;
server_name "i2.test.com";
root /var/www/test2.com/html;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
}
The servers are fake to serve static files - I do not have real ones. When I enable logging - I see correct output:
{"evaluation_response" : "http://127.0.0.1:8088", "for instance" : "instance1"}
{"redirect" : "http://127.0.0.1:8088/?-"}
{"evaluation_response" : "http://127.0.0.1:8089", "for instance" : "instance2"}
{"redirect" : "http://127.0.0.1:8089/?-"}
But page never loads (Chrome message):
The ::IP:: page isn’t working ::IP:: didn’t send any data.
ERR_EMPTY_RESPONSE
The error.log does not contain any useful information (output level - debug), but if you need it - I can provide a copy for one request.
What is wrong with my configuration?
Related
server {
listen 80;
listen [::]:80;
server_name _;
root /home/ec2-user;
# location / {
# proxy_pass http://sdc_servers;
# }
location /loaderio-73cc9f7580b0a0844a502ff1c98e9305.txt {
proxy_pass http://3.15.28.77/loaderio-73cc9f7580b0a0844a502ff1c98e9305/;
}
}
I am not sure where I am going wrong trying to serve this file, I am totally lost
location /loaderio-73cc9f7580b0a0844a502ff1c98e9305.txt { proxy_pass http://3.15.28.77/loaderio-73cc9f7580b0a0844a502ff1c98e9305/; }
Instead of passing the token in the proxy_pass, try something like
location /loaderio-9f8a9889007f8a2721b6245b80344b75.txt { root /<location_to_the_file>/loader/; try_files /loader.txt =404; }
loader - directory containing loader.txt which contains the actual
token provided by loader.io.
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I have a server with Nginx.
I would like to set up two sites:
backend.mysite.com
staging.backend.mysite.com
Here is my server blocks config:
www.backend.mysite.com:
server {
listen 80;
server_name backend.mysite.com www.backend.mysite.com;
location / {
proxy_pass http://127.0.0.1:8800/;
}
}
server {
listen 8800;
server_name my.ip.address;
root /projects/backend/production/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
www.staging.backend.mysite.com:
server {
listen 80;
server_name staging.backend.mysite.com www.staging.backend.mysite.com;
location / {
proxy_pass http://127.0.0.1:8900/;
}
}
server {
listen 8900;
server_name my.ip.address;
root /projects/backend/staging/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
My problem is that backend.mysite.com is overriding staging.backend.mysite.com. How can I say to Nginx to never override if there is a sub subdomain on my adress?
UPDATE:
I've tried to add another domain (my_other_site.com) in my second config to check if it works:
server {
listen 80;
server_name my_other_site.com www.my_other_site.com staging.backend.mysite.com www.staging.backend.mysite.com;
location / {
proxy_pass http://127.0.0.1:8900/;
}
}
server {
listen 8900;
server_name my.ip.address;
root /projects/backend/staging/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
When I visit my_other_site.com it works well. The second site is reached as expected.
my_other_site.com, www.my_other_site.com, staging.backend.mysite.com and www.staging.backend.mysite.com have the same DNS A configuration, they are pointing on the same IP.
UPDATE 2:
When I disable www.backend.mysite.com server block, it works. The site staging.backend.mysite.com is working as expected. That mean that indeed the first block overrides the second one.
How can I tell the first server block to not take in account staging.backend.mysite.com? Is there a way to exclude a specific domain name?
Try to give for included config files same names as domain names, e.g.:
aa.domain.com --> aa.domain.com.nginx.conf
bb.domain.com --> bb.domain.com.nginx.conf
so nginx will include and catch in natural alphabetic order
I was running into the same experience, but eventually when I ran sudo nginx -T, it spat out:
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
nginx: configuration file /etc/nginx/nginx.conf test failed
So, I edited nginx.conf and uncommented the relevant line:
# ...
server_names_hash_bucket_size: 64;
# ...
I picked 64 because that was what was in the file, commented out. Then I restarted and everything worked.
Then I cleared the cache in my browser.
I want nginx to search my local host for the file first and on a 404 error it should search server 1.1.1.1.
I am able to fetch the file that is located on local host, but not able to get from server 1.1.1.1.
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log main;
location /products/ {
proxy_next_upstream http_404;
root /var/foo;
}
}
server {
listen 80;
server_name 1.1.1.1;
location /products/ {
#########
}
}
I guess proxy_next_upstream is not switching to the server.
Any help on this would be appreciated.
The proxy_next_upstream directive is a configuration directive to control re-request from a group of upstream servers by a proxy_pass if request to one of them fails. It doesn't make sense without proxy_pass and an upstream block defined. You may use it if you proxy to multiple upstream servers like this:
upstream backends {
server 192.2.0.1;
server 192.2.0.2;
...
}
server {
...
location / {
proxy_pass http://backends;
proxy_next_upstream error timeout http_404;
}
}
If you want nginx to search for a file on disk, and if it's not found - proxy request to another server, configure it e.g. using try_files fallback instead:
location / {
root /path/to/root;
try_files $uri #fallback;
}
location #fallback {
proxy_pass http://...
}
See http://nginx.org/r/try_files for more info about the try_files directive.