my nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream app_servers {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}
server {
listen 6200;
server_name test;
add_header X-GG-Cache-Status $upstream_cache_status;
include rewrite.conf;
}
}
and my rewrite.conf in the same folder as that
location = / {
rewrite ^/some-custom-destination/?$ /destination/detail?id=33;
proxy_pass http: //app_servers;
proxy_intercept_errors on;
error_page 400 404 /;
error_page 500 502 503 504 /error.html;
location = /error.html {
root /etc/nginx/;
}
}
when I use nginx -s reload command getting that error : nginx: [emerg] unknown directive "location" in /etc/nginx/rewrite.conf:1
How can I fix that?
Help, please. Thank you.
Apart from the space in your proxy_pass directive, there is one more issue with your location block.
From the nginx documentation about nginx location directive (http://nginx.org/en/docs/http/ngx_http_core_module.html#location), you cannot have a nested location inside a location block “location = /”.
“Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.”
Related
I'm trying to setup nginx to separate client_max_body_size in one location per http method, but client_max_body_size isn't working with "if" and "limit_except":
1) Config:
location /test {
limit_except POST {
client_max_body_size 1g;
}
proxy_pass ...
}
nginx -s reload:
nginx: [emerg] "client_max_body_size" directive is not allowed here
2) Config:
location /test {
if ($request_method !~* POST) {
client_max_body_size 1g;
}
proxy_pass ...
}
I get the same message on reload.
How can I set client_max_body_size per http method?
Maybe you can solve this by defining an upstream and use proxy_pass to redirect at the right condition:
http://nginx.org/en/docs/http/ngx_http_upstream_module.html
I would proxy a request like the upstream solution.
server_name ~^(?<subdomain>.+)\.example\.com$;
root /dev/null;
location / {
error_page 502 #nextserver;
resolver 127.0.0.1:53 valid=300s;
proxy_pass "https://$subdomain";
}
location #nextserver {
error_page 502 #error;
resolver 127.0.0.1:53 valid=300s;
proxy_pass "https://$subdomain-blahblah";
}
location #error {
return 502 'Service is not available';
}
As you can see I would check https://$subdomain and if it doesn't exist or it down then checks https://$subdomain-blahblah.
it works fine but the problem happen when the second server is down, then Nginx doesn't provide Service is not available message.
So the scenario is like
check Server A -> down
check Server B -> down
Return custom error
I couldn't use upstream because the name of servers is dynamic.
I would suggest you to solve this problem differently. Check out this sample. It shows how you can approach the problem:
http{
listen 80;
root /path/to/static/files;
upstream_server subdomain1{
server 192.168.1.100:8000;
server 192.168.1.101:8000;
}
upstream_server subdomain2{
server 192.168.1.102:8000;
server 192.168.1.103:8000;
}
location / {
server_name subdomain1.example.com;
proxy_pass http://subdomain1;
include /etc/nginx/proxy_pass;
}
location / {
server_name subdomain2.example.com;
proxy_pass http://subdomain2;
include /etc/nginx/proxy_pass;
}
}
I'm trying to load balance a web application through nginx, It works fine for all will my web application calls a service with sub-path.
for example it works
http://example.com/luna/
but not for
http://example.com/luna/sales
My nginx.conf
user nobody;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream lunaups {
server myhostserver1.com:8080;
server myhostserver2.com:8080;
}
server {
listen 80;
server_name example.com;
proxy_pass_header Server;
location = / {
rewrite ^ http://example.com/luna redirect;
}
location /luna {
rewrite ^$/luna/(.*)/^ /$1 redirect;
proxy_pass http://lunaups;
#add_header X-Upstream $upstream_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
my web application calls a service with additional subpath like /luna/sales fails to return response. What am i missing here?
It works if i remove one of my host server from upstream, But when I add second host on upstream it fails to return response.
Is my rewrite rule wrong or my configurations as whole is wrong?
There are four suffixes to the rewrite directive and they all have specific uses. See this document for details.
If you want the URI / to be mapped to /luna without changing the URL in the browser, you would use an internal rewrite with rewrite ... last. For example:
location = / {
rewrite ^ /luna last;
}
In the location /luna block, you need to rewrite the URI before sending it to proxy_pass statement (without leaving the location block), which requires a rewrite ... break. For example:
location /luna {
rewrite ^/luna(/.*)$ $1 break;
rewrite ^ / break;
proxy_pass http://lunaups;
}
The first rewrite changes any URI with a sub-path, and the second rewrite handles the URI without a sub-path.
See this useful resource on regular expressions.
I need to proxy a couple of urls to different hosts. Actually, I'm using the same host with different port to test my nginx configuration. This is my virtual host definition:
server {
listen 8081;
server_name domain.com;
location /Plasmid/ {
proxy_pass http://localhost:8000/Plasmid/;
}
location /_community/ {
proxy_pass http://localhost:8082/comments_api/ ;
}
location / {
# rewrite cq_user_authenticated===(.*)/(.*)/iuuid=(.*)/commenti.html$ /Plasmid/comments/legacy/$3/$1 ;
# rewrite querystring===(.*)$ /Plasmid/comments/legacy/$1 ;
# rewrite cq_user_authenticated===([^&]*)&/.*uuid=([^/]*) /comments_api/legacy/$2 ;
# rewrite userdetails(.*) /Plasmid/comments/user_details ;
root html;
index index.html index.htm;
}
}
Of course my hosts file has mapping for the domain.com
When I call the url: http://domain.com:8081/Plasmid/default/page/12 I get an http 404
If I remove the second location from my configuration:
location /_community/ {
proxy_pass http://localhost:8082/comments_api/ ;
}
I get the resource I want, but some part are missed since the are hosted on a different platform:
[error] 1033#0: *1 open() "/usr/local/Cellar/nginx/1.2.6/html/_community/content
How can I resolve this issue?
Do a little change:
location ^~ /Plasmid/ {
proxy_pass http://localhost:8000/Plasmid/;
}
location ^~ /_comunity/ {
proxy_pass http://localhost:8082/comments_api/;
Why is that? Because ^~ means starts with and when you request for page:
http://domain.com:8081/Plasmid/default/page/12
it fit to that rule. In your configuration you are using no mark and something like this:
location /anylocation
and it looks like your nginx prefer rule
location / {
than
location /Plasmid
and
location /_comunity
because it's using root directive and searching for _community/content in html folder (as you get in error message).
In other words ^~ has greater priority than no mark. One thing that could also help is to add break directive after each proxy_pass directive;
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.