In Nginx how to disable caching for 301 but caching for 200 response.
I tried the below configuration but if seem to not cache 200 and 301.
location = / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto $scheme;
proxy_pass http://app:3000;
add_header X-GG-Cache-Status $upstream_cache_status;
proxy_no_cache $http_response_status 301;
}
Related
I have a script that deploys nginx configuration to some reverse proxies. My nginx configuration looks like this
server {
listen 443 ssl;
server_name SOME_NAME;
ssl_certificate MYCERT.crt;
ssl_certificate_key MYKEY.key;
location /health-check {
add_header Content-Type text/plain;
return 200 'healthy\n';
}
location /prod/ {
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_pass https://${PROD_URL}/;
proxy_read_timeout 90;
}
location /integration/ {
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_pass https://${INTEGRATION_URL}/;
proxy_read_timeout 90;
}
location /dev/ {
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_pass https://${DEV_URL}/;
proxy_read_timeout 90;
}
}
My problem is that my requests are passed as follows:
10.211.194.44 - - [08/Mar/2022:11:08:42 +0000] "GET /devSometihing/test HTTP/1.1" 404 169 "-" "curl/7.76.1" "10.211.194.71" "-"
what I'd like is anything to goes to the /dev location being proxypassed as /whateverIsPassedHere instead as /devwhateverIsPassedHere
How can I manipulate the incoming request to only pass to the backend servers whatevers is passed after /dev/ or /prod/ or /integration ???
i need to implement this configuration in my nginx web server ( reverse_proxy ):
I have a domain like www.domainA.it where respond my site, then i need to publish antother "app" from my www.domainA.it/appA, the application respond to another domain like www.domainB.it/appB
The nginx configuration that i have implement is like this:
server {
listen 443;
server_name www.domainA.it domainA.it;
error_log ssl_error.log;
access_log ssl_access.log;
ssl on;
ssl_certificate my.crt;
ssl_certificate_key my.key;
include /etc/nginx/sec.conf;
add_header 'Access-Control-Allow-Origin' *;
location / {
add_header 'Access-Control-Allow-Origin' *;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass https://my-backend;
}
location /appA {
proxy_pass https://domainB/appB/;
proxy_http_version 1.1;
add_header Access-Control-Allow-Origin *;
proxy_set_header Host $proxy_host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
With this configuration i recive the error like this:
https://www.domainA.it/appB/... 404 not found
https://www.domainA.it/appB/... 404 not found
.
.
.
If i expose the same location www.domainA/appB , all it's work fine
How i can do this ??
Thanks a lot
i have resolved this issue by modifing the backend context.
It works all fine with this configuration:
location /appA {
proxy_pass https://domainB/appA/;
proxy_http_version 1.1;
add_header Access-Control-Allow-Origin *;
proxy_set_header Host $proxy_host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
I'm configuring nginx with this config:
location /test {
proxy_pass http://127.0.0.1:10000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
It works, but without all assets. I inspect element, the assets redirect to http://127.0.0.1:10000 (absolutely 404), where it should be http://127.0.0.1:10000/test/asset.css.
Need advice :)
PS: My server is using angular2 (npm start)
May the force be with you:
location / {
proxy_pass http://127.0.0.1:10000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /root/of/your/public/assets;
access_log off;
expires max;
}
I have two hello servers in server1(1.1.1.1) and server2(1.1.1.2). Now I want to use Nginx(example.com) to proxy pass request to the certain server like this:
Request Url:http://example.com/hello1
proxy pass:http://1.1.1.1/hello
Reqeust Url:http://example.com/hello2
proxy pass:http://1.1.1.2/hello
Just add the location block to example.com config. This should work.
location ^~ /hello1 {
proxy_set_header Proxy "";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_pass http://1.1.1.1/hello;
proxy_redirect off;
}
location ^~ /hello2 {
proxy_set_header Proxy "";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_pass http://1.1.1.2/hello;
proxy_redirect off;
}
Here is my site conf -
upstream demo {
server server1:31337;
server server2:31338;
server server3:31339;
server server4:31340;
keepalive 64;
}
server {
listen 80;
server_name www.site.org;
rewrite ^ https://$server_name$request_uri? permanent;
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
proxy_pass http://demo;
}
}
I want when I open www.site.org it should proxy pass to any of the upstream server as server2:31337/mysite
How do I append /mysite request paramater ?
Can you try this
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
rewrite ^(.*)$ /mysite$1 break;
proxy_pass http://demo;
}