I have a sub-domain (foo.domain.com) that is maintained by my organization and in which I don't have control.
The organization configured that domain to point to a Route 53 service and from there I created a policy that sends the request to an ALB which then finally sends it to an EC2 running NGINX.
NGINX will be responsible for URL rewrites to many other services I have running in different EC2 instances.
What I want is, for example:
request is sent to foo.domain.com/service1
goes to Route53 then to an ALB and then to the EC2 running NGINX
in NGIX, I have the following config
...
location = /service1/
{
proxy_redirect off;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_pass https://public_ip_of_service1_ec2_instance$request_uri;
}
This request goes to the EC2 instance running service1, which is another NGINX proxy running a node.js application (npm build kinda thing)
The NGINX service1 conf file is:
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/self-signed.conf;
server_name public_ip_of_service1_ec2_instance;
root /app; #this is where the static files are hosted
location ~* \.html?$ {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, must-revalidate";
}
location / {
add_header Content-Security-Policy "default-src 'self' 'unsafe-inline';";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN";
index index.html;
try_files $uri $uri/ /index.html;
# kill cache
# expires -1;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
If I hit the NGINX URL of service1, it works just fine, but the problem is when I call the NGINX proxy from step 3.
When I hit the URL foo.domain.com/service1 on the browser, I see this:
Basically, it says that foo.domain.com/js/chunk....js can't be found. This is correct since this file is being served in the EC2 that is running that node.js application (that is, it can be found at http://public_ip_of_service1_ec2_instance/js/chunk....js)
What is the configuration that I am missing in order to be able to keep the domain foo.domain.com/service1 for my users at all times and never to see the public_ip_of_service1_ec2_instance
Thank you
EDIT 1
The issue reported here seems to be exactly like mine: https://serverfault.com/questions/805836/reverse-proxy-application-and-its-static-files-with-nginx
I followed the suggestion there and as well as #anemyte suggestion but it is not working.
It seems like Proxy1 is trying to serve the static files that are hosted in Proxy2 as you can see here:
proxy | 2021/01/04 10:16:22 [error] 29#29: *4 open() "/etc/nginx/html/js/app.53272292.js" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /js/app.53272292.js HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | ( - - ) 172.19.0.1 - - [04/Jan/2021:10:16:22 +0000] "GET /css/app.0988b263.css HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | ( - - ) 172.19.0.1 - - [04/Jan/2021:10:16:22 +0000] "GET /css/chunk-vendors.e78a06e5.css HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | 2021/01/04 10:16:22 [error] 29#29: *4 open() "/etc/nginx/html/css/app.0988b263.css" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /css/app.0988b263.css HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | 2021/01/04 10:16:22 [error] 29#29: *6 open() "/etc/nginx/html/css/chunk-vendors.e78a06e5.css" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /css/chunk-vendors.e78a06e5.css HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | 2021/01/04 10:16:23 [error] 29#29: *6 open() "/etc/nginx/html/js/chunk-vendors.7b27d7ff.js" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /js/chunk-vendors.7b27d7ff.js HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | ( - - ) 172.19.0.1 - - [04/Jan/2021:10:16:23 +0000] "GET /js/chunk-vendors.7b27d7ff.js HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | 2021/01/04 10:16:23 [error] 29#29: *6 open() "/etc/nginx/html/js/app.53272292.js" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /js/app.53272292.js HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | ( - - ) 172.19.0.1 - - [04/Jan/2021:10:16:23 +0000] "GET /js/app.53272292.js HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
My proxy 1 conf file looks like this now:
worker_processes 1;
events { worker_connections 1024; }
http {
log_format main '( $proxy_host $upstream_addr ) $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
rewrite_log on;
access_log /var/log/nginx/access.log main;
server {
listen 80;
server_name apacbsa.com;
location /auspost/ {
# proxy_redirect off;
# proxy_read_timeout 1m;
# proxy_connect_timeout 1m;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# proxy_buffering off;
#proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-Host $server_name;
proxy_pass https://192.168.1.113/;
}
}
}
EDIT2
Still no luck. This is what I am seeing in NGINX1 logs:
proxy | ( 192.168.1.113 192.168.1.113:443 ) 172.19.0.1 - - [05/Jan/2021:00:00:37 +0000] "GET /auspost/ HTTP/1.1" 200 2111 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | ( - - ) 172.19.0.1 - - [05/Jan/2021:00:00:37 +0000] "GET /css/chunk-vendors.e78a06e5.css HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | 2021/01/05 00:00:37 [error] 21#21: *9 open() "/etc/nginx/html/css/chunk-vendors.e78a06e5.css" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /css/chunk-vendors.e78a06e5.css HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | ( - - ) 172.19.0.1 - - [05/Jan/2021:00:00:37 +0000] "GET /js/app.54337cb4.js HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | ( - - ) 172.19.0.1 - - [05/Jan/2021:00:00:37 +0000] "GET /css/app.0988b263.css HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | 2021/01/05 00:00:37 [error] 21#21: *12 open() "/etc/nginx/html/js/app.54337cb4.js" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /js/app.54337cb4.js HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | 2021/01/05 00:00:37 [error] 21#21: *11 open() "/etc/nginx/html/css/app.0988b263.css" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /css/app.0988b263.css HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | 2021/01/05 00:00:37 [error] 21#21: *13 open() "/etc/nginx/html/js/chunk-vendors.7b27d7ff.js" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /js/chunk-vendors.7b27d7ff.js HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | ( - - ) 172.19.0.1 - - [05/Jan/2021:00:00:37 +0000] "GET /js/chunk-vendors.7b27d7ff.js HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | ( - - ) 172.19.0.1 - - [05/Jan/2021:00:00:38 +0000] "GET /css/app.0988b263.css HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | ( - - ) 172.19.0.1 - - [05/Jan/2021:00:00:38 +0000] "GET /css/chunk-vendors.e78a06e5.css HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | 2021/01/05 00:00:38 [error] 21#21: *12 open() "/etc/nginx/html/css/app.0988b263.css" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /css/app.0988b263.css HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | 2021/01/05 00:00:38 [error] 21#21: *11 open() "/etc/nginx/html/css/chunk-vendors.e78a06e5.css" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /css/chunk-vendors.e78a06e5.css HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | ( - - ) 172.19.0.1 - - [05/Jan/2021:00:00:39 +0000] "GET /js/chunk-vendors.7b27d7ff.js HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
proxy | 2021/01/05 00:00:39 [error] 21#21: *13 open() "/etc/nginx/html/js/chunk-vendors.7b27d7ff.js" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /js/chunk-vendors.7b27d7ff.js HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | 2021/01/05 00:00:39 [error] 21#21: *11 open() "/etc/nginx/html/js/app.54337cb4.js" failed (2: No such file or directory), client: 172.19.0.1, server: apacbsa.com, request: "GET /js/app.54337cb4.js HTTP/1.1", host: "localhost", referrer: "http://localhost/auspost/"
proxy | ( - - ) 172.19.0.1 - - [05/Jan/2021:00:00:39 +0000] "GET /js/app.54337cb4.js HTTP/1.1" 404 153 "http://localhost/auspost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "-"
And this is what I see in the NGINX2 (serving static files) logs (just 1 line):
frontend | 172.21.0.1 - - [05/Jan/2021:11:04:17 +1100] "GET / HTTP/1.0" 200 2111 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0" "172.19.0.1"
This is the full nginx conf file for NGINX2:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 10m;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;
resolver_timeout 600s;
# for DDOS protection - 3 requests per second
limit_req_zone $binary_remote_addr zone=one:10m rate=3r/s;
# limiting the number of connections one client can make
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 80;
listen [::]:80;
server_name apacbsa.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
include snippets/self-signed.conf;
server_name apacbsa.com;
root /app;
location ~* \.html?$ {
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, must-revalidate";
}
location /auspost/ {
add_header Content-Security-Policy "default-src 'self' 'unsafe-inline';";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN";
index index.html;
try_files $uri $uri/ /index.html;
# kill cache
# expires -1;
}
location /api {
# limiting the number of api calls
limit_req zone=one burst=2 nodelay;
# only one connection per client to the api
limit_conn addr 1;
proxy_pass https://backend:5001/api;
proxy_redirect off;
proxy_connect_timeout 1m;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
Everything is running in docker containers, but that shouldn't be the problem I don't think.
So I issue the request from my browser in my Mac laptop and in that laptop I am running the NGINX1 proxy. That then forwards to my Windows machine running NGINX2 static files.
When you declare a location like that:
location = /service1/
It means that you want exact URL match. So it will only work for foo.domain.com/service1/ while foo.domain.com/service1/something won't match this location. Looking at the URL of your request (/js/chunk) this isn't what you want, so remove = from the location:
location /service1/
Next is the server_name. Your first proxy passes Host header which NGINX uses to determine which vhost configuration to use. The Host header value should be foo.domain.com but your server_name on the second NGINX is public_ip_of_service1_ec2_instance. It doesn't matter if the vhost is a default one but I don't see that in your configuration.
You can add foo.domain.com after public_ip_of_service1_ec2_instance or just replace one with the other. What's best in this case you should decide yourself and if you wish to learn more about server names I recommend you reading these great articles: How nginx processes a request and Server names
UPD: About missing static files.
You need to change base URL or base path of your service1 application from / to /service1/. This is not related to NGINX, unless your application is made of just static files.
Why did that happen? Well you've told first NGINX that you want all requests with URL starting with /service1/ to be forwarded to service1. When a client hits service1, the page refers some static, /js/chunk-vendors.7b27d7ff.js for example. Client's browser makes an additional request to the first NGINX asking to provide /js/chunk-vendors.7b27d7ff.js. Since the URL does not begin with /service1/ NGINX does not forward the request to /service1/ and it uses another piece of config for this request (/ most likely).
If you change all absolute URLs at service1 to begin with /service1/ the problem should go. A dangerous alternative to that would be removing first slash from absolute links. This way URLs will be relative to current location and not website root. Think twice before going second option.
I have done some testing lately and here's what I found out:
In your nginx proxy config, remove the line:
proxy_set_header Host $http_host;
Which will result in your final configuration nginx file to be:
location = /
{
proxy_redirect off;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_pass https://public_ip_of_service1_ec2_instance;
}
You could configure something like this in the first nginx server
server {
listen 80;
listen [::]:80;
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
server_name domain1;
if ($request_method ~* OPTIONS|GET|HEAD) {
return 301 https://domain2$request_uri;
}
location ~* api {
proxy_pass https://domain2$request_uri;
}
}
I want to use nginx as forward proxy, but rewrite (also the host part) the URL based on a header value.
Suppose the browser connect to nginx on port 8888 with a regular http request. The header ha the pair:
X-myattribute: https://somehost.com
nginx should proxy_pass to https://somehost.com
My nginx.conf is now:
server {
listen 8888;
proxy_connect;
proxy_max_temp_file_size 0;
resolver 8.8.8.8;
location / {
proxy_pass https://$http_myattribute;
# proxy_pass http://$http_host$uri$is_args$args;
proxy_set_header Host $http_host;
}
}
}
but I get:
2018/08/16 19:44:08 [error] 9#0: *1 invalid port in upstream "https://somehost.com:443", client: 172.17.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8888"
2018/08/16 19:47:25 [error] 9#0: *1 invalid URL prefix in "https://somehost.com:443", client: 172.17.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8888"
(two lines depending if I set proxy_pass http://$X-myattribute or proxy_pass https://$X-myattribute or proxy_pass $X-myattribute. Assume X-myattribute always have http:// or https://)
Any suggestion?
I follow here to setup separated front-end and back-end in centos 7 (with docker), using nginx to host my front-end, my website works, and all /api/.... requests work, however, when I want to check RESTful API serviced by swagger, got a 404 error, fiddler says:
not found /swagger-ui/index.html
I open 8080 on host and navigate the swagger-ui folder and index.html is just there. and all www folder is given 755 permission.
The nginx site.conf is as below:
server {
listen 80;
index index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
location / {
root /usr/share/nginx/html;
}
location /api {
proxy_pass http://www.example.com:8080/api;
}
location /management {
proxy_pass http://www.example.com:8080/management;
}
location /v2 {
proxy_pass http://www.example.com:8080/v2;
}
location /swagger-ui {
proxy_pass http://www.example.com:8080/swagger-ui;
}
location /swagger-resources {
proxy_pass http://www.example.com:8080/swagger-resources;
}
}
What's wrong?
===============================updated
after issue:
sudo docker logs -f -t --tail 100 docker_nginx_1
found somethings wrong in log file:
2018-07-14T17:35:40.904857522Z 187.67.50.246 - - [14/Jul/2018:17:35:40 +0000] "GET /cgi/common.cgi HTTP/1.1" 404 169 "-" "Wget(linux)" "-"
2018-07-14T17:35:40.904920067Z 2018/07/14 17:35:40 [error] 5#5: *23 open() "/usr/share/nginx/html/cgi/common.cgi" failed (2: No such file or directory), client:187.67.50.246, server: localhost, request: "GET /cgi/common.cgi HTTP/1.1", host: "39.107.246.223"
2018-07-14T17:35:43.896984659Z 187.67.50.246 - - [14/Jul/2018:17:35:43 +0000] "GET /stssys.htm HTTP/1.1" 404 169 "-" "Wget(linux)" "-"
It works after I comment out:
location /swagger-ui {
proxy_pass http://www.example.com:8080/swagger-ui;
}
I thought swagger-ui was served by nginx instead of back-end.
I moved from drupal to jekyll using nginx as webserver
I've got old urls prefixed with /?q=permalink
I'me trying to rewrite them but it fails, and I don't understand why.
example :
http://my.domain.net/?q=my-post-permalink
has to be redirected to
http://my.domain.net/my-post-permalink
my default.conf :
location / {
root /usr/share/nginx/html;
index index.html index.htm;
rewrite ^/\?q=(.*)$ $1 break;
try_files $uri $uri/ $uri.html =404;
}
logs :
2018/01/14 09:42:17 [notice] 5#5: *1 "^/\?q=(.*)$" does not match "/", client: 127.0.0.1, server: localhost, request: "GET /?q=my-post-permalink HTTP/1.1", host: "my.domain.net"
2018/01/14 09:42:17 [notice] 5#5: *1 "^/\?q=(.*)$" does not match "/index.html", client: 127.0.0.1, server: localhost, request: "GET /?q=my-post-permalink HTTP/1.1", host: "my.domain.net"
127.0.0.1 - - [14/Jan/2018:09:42:17 +0000] "GET /?q=my-post-permalink HTTP/1.1" 200 3520 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" "-"
2018/01/14 09:42:17 [notice] 5#5: *1 "^/\?q=(.*)$" does not match "/css/foundation.css", client: 127.0.0.1, server: localhost, request: "GET /css/foundation.css HTTP/1.1", host: "my.domain.net", referrer: "http://my.domain.net/?q=my-post-permalink"
The rewrite directive uses a normalised URI that is stripped of the query string. The query string can be accessed using the $arg_ family of variables. See this document for more.
You are only interested in rewriting the q parameter of the / URI, so an exact match location block should be used. See this document for more.
For example:
root /usr/share/nginx/html;
index index.html index.htm;
location = / {
if ($arg_q) {
return 301 /$arg_q;
}
}
location / {
try_files ...;
}
See this caution on the use of if.
I am trying to run Akka service using nginx. Here is my default.conf:
upstream hello-akka{
server localhost:9000;
}
server {
listen 9000;
location /* {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /akka {
proxy_pass http://hello-akka;
}
location /assets {
root /var/www;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Default nginx page works just fine but when I redirect to /akka I get 500 internal server error. Here is my nginx log file:
2017/12/05 10:58:17 [crit] 11077#11077: *1014 open() "/usr/share/nginx/html/50x.html" failed (24: Too many open files), client: 127.0.0.1, server: , request: "GET /akka HTTP/1.0", upstream: "http://127.0.0.1:9000/akka", host: "hello-akka"
I have changed /etc/security/limits.con file to increase the number of connections from 1024 to 16384. However, after that I keep getting another error:
host: "hello-akka"
2017/12/05 11:40:42 [error] 15916#15916: *37494 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /akka HTTP/1.0", upstream: "http://127.0.0.1:9000/akka"
So what is wrong with my configurations and how should I change them to run /akka page normally?
UPDATE: I have changed server localhost:9000 to server localhost:8080 in upstream hello-akka. However, when I redirect to localhost:9000/akka I get An error occurred page.
Error logs shows this error:
*1 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /akka HTTP/1.1", upstream: "http://127.0.0.1:8080/akka", host: "localhost:9000"