How to proxy to another URL without redirect - nginx

Is it possible to use nginx to work like this:
When going to x.com/blog -> display the content of blog.x.com (with all the query parameters, etc) without changing the browser URL (without browser redirecting)
I tried
location /blog {
proxy_pass https://blog.x.com
}
but that didn't work

This nginx config works for me.
server {
listen 80;
listen [::]:80;
server_name x.com;
location /blog {
proxy_pass https://blog.x.com;
proxy_set_header Host blog.x.com;
}
}

Related

How to correctly change the location directive in nginx?

I implemented a simple REST API using Falcon and running it with Gunicorn. An example call looks like this
curl "http://localhost:5000/articles?limit=100"
Now I'm trying to make the API accessible using nginx, and I actually got it working using the following config file for nginx
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
location / {
include proxy_params;
proxy_pass http://localhost:5000;
}
}
With this I can go to http://xxx.xxx.xxx.xxx/articles?limit=100 to get the respective response. The only thing I would like to do now is to change the location directive. When I change the config file to
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
location /test/ {
include proxy_params;
proxy_pass http://localhost:5000;
}
}
I would assume that http://xxx.xxx.xxx.xxx/test/articles?limit=100 will again give me the correct response, but I get an 404. Using /test instead of /test/ doesn't help either. What am I missing?

How can a request resolve to a different domain while keeping URI information?

I have 2 domains domain.xyz and domainxyz.com
I have the boilerplate for a dynamic routing service set up on domain.xyz which serves domain.xyz/example-uri.
I would like to make domainxyz.com/example-uri resolve to domain.xyz/example-uri
On the server I am using Nginx to direct requests on port 80 to the React app
instead of using a dns redirect to resolve the .com to the .xyz (which is what I was initially doing), direct both domains to the nginx server. Then configure the nginx server like this:
source/explanation: https://www.nginx.com/blog/creating-nginx-rewrite-rules/
server {
listen 80;
server_name www.domainxyz.com domainxyz.com;
location / {
return 301 $scheme://domain.xyz$request_uri;
proxy_pass http://localhost:3000;
}
}
server {
listen 80;
server_name www.domain.xyz domain.xyz;
location / {
proxy_pass http://localhost:3000;
}
}

NGINX ignoring root when using proxy pass

I'm having a bit of a problem with NGINX. I made a simple HTML that loads my application in a iframe hosted on http://localhost:3000.
Using the config below, localhost:3000 is proxied but root /var/www is ignored so I only get to see the application without any of my html.
What would be the correct way to proxy a url loaded inside an iframe?
server {
listen 80;
listen [::]:80;
root /var/www/sub.mydomain.com/html;
index index.html index.htm index.nginx-debian.html;
server_name sub.mydomain.com;
location / {
proxy_set_header Host $host;
proxy_pass http://localhost:5601/;
}
}

Redirect all http to https in nginx, except one file

I am currently running my site on http, and want to move it over to https such that nginx handles the redirection automagically. This is fairly trivial to do, I guess.
However, there is one file that (for several reasons) is hot-linked from other sites, some of which are over http and some over https. I want to ensure that the file is available over both http and https, so as to ensure that browsers don't complain with the "mixed content" dialog. The path of the file looks something like this:
http(s)://mydomain.com/scripts/[some_sha1_hash]/file.js
So, the nginx rule should say: "If the request is already over https, everything is sweet, and just reverse-proxy it. Otherwise, redirect all requests from http to https, except if this one file is requested, in which case don't do any such http->https redirect."
Can anyone either tell me where to look to learn about such a config, or help me with the config itself? Thanks in advance. (I'm sorry, but I'm not skilled enough yet at nginx configuration.)
This is what I did, which works:
server {
listen 80;
server_name example.com;
charset utf-8;
access_log /var/www/path/logs/nginx_access.log;
error_log /var/www/path/logs/nginx_error.log;
location /path/to/script.js {
# serve the file here
}
location / {
return 301 https://example.com$request_uri;
}
}
This one handles only http requests and serves the said file - otherwise redirects to https. Define your ssl server block, which will serve all https requests.
server {
listen 443;
server_name example.com;
ssl on;
# rest of the config
}
This way your script file will be available on http as well as https.
Try this:
server {
listen 80; ssl off;
listen 443 ssl;
server_name example.com;
# <ssl settings>
# ... other settings
location = /scripts/[some_sha1_hash]/file.js {
# Empty block catches the match but does nothing with it
}
location / {
if ($scheme = "http") {
rewrite ^ https://$http_host$request_uri? permanent;
}
# ... other settings
}
}
server {
listen 80;
server_name my.domain.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443;
server_name my.domain.com;
ssl on;
[....]
}
The above should mostly do the trick if im not wrong

NGINX passing requests to Pylons and relative URLs

I have NGINX running on port 8080. I have the following setup in my NGINX conf file.
server {
listen 8080;
server_name domain.com;
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
proxy_pass http://127.0.0.1:80;
proxy_redirect http://127.0.0.1:80 http://domain.com;
}
These rules work correctly as far as I can tell. The only issue I run into is when Pylons gets a request for a relative URL it is using http://127.0.0.1/linkto/something instead of http://domain.com:8080/linkto/something. I believe I am missing something in my Pylons configuration, if you have any advice or need additional information just let me know. Thanks in advance for any assistance on this.
By default, proxy_pass uses the hostname from the directive (127.0.0.1 in your case) as the Host: header for its request. You probably just need to add proxy_set_header Host $http_host; to have it pass through the original Host header to your backend.

Resources