I have few domains pointing to the same server, for example:
server_name www.domain1.dev www.domain2.dev www.domain3.dev
Most of the configurations are the same, and they are pointing at the same php file, but i have a different assets folder for each domain:
/www.domain1.dev/assets/
/www.domain2.dev/assets/
/www.domain3.dev/assets/
that's why i want to rewrite requests like http://www.domain1.dev/assets/* into http://www.domain1.dev/www.domain1.dev/assets/*
Was trying to play with location - return, but didn't find how to get current domain name.
The currently requested domain name can be in $host or $http_host. See this document for the differences.
For example:
location ^~ /assets {
root /path/to/root/$host;
}
Or:
location ^~ /assets {
return 301 /$host$request_uri;
}
An alternative approach would be to separate your domain names into separate server blocks and use an include statement to pull in the common content.
Related
According to many old posts here and in other places on the internet, the following nginx configuration should proxy http://nginx-service/foo/bar to http://web.default.svc.cluster.local:8080/bar.
In other words, it should strip the /foo part in the path (the location matched) while appending the rest (/bar) when proxying it on.
That is not what I observe in practice; the full path is removed and / is proxied on.
How could I proxy this to the upstream service while keeping /bar?
You didn't add any config in your question but any of the following should work:
location /foo/ {
proxy_pass http://web.default.svc.cluster.local:8080/;
}
or
location /foo/bar {
proxy_pass http://web.default.svc.cluster.local:8080/bar;
}
I want NGINX to serve serve files from a location within a server.
As an example, I would like the url http://domain/ss/image.png to serve the file located within /home/data/screenshots/image.png
So far, I have attempted to use a regex in this manner
location ~ ^/ss/(.*) {
root /home/data/screenshots;
add_header content-type "image/png";
try_files $1 /$1;
}
however it appears that this location is never reached, being handled by the location spefcified to / (which in my case is a redirect).
I am not flexible with renaming/changing any of the file structure of the project and want to achieve this result with just the NGINX config modification.
As described by Richard's comment on the question, it appears that my regex approach was correct, however my issue was the usage of the try_files function.
Is it possible for me to have an nginx server's domain to contain slashes? For example, for the server's root url location to be https://example.com/apps/app1?
I have a server whose files need to be served from /opt/production/app1/public. My current nginx.conf configuration, which doesn't work, contains:
location /apps/app1 {
root /opt/production/app1/public;
}
But obviously, this doesn't work because my files aren't at /opt/production/app1/public/apps/app1. I would like for nginx to consider https://example.com/app/apps1 to be my domain, so that my nginx.conf can access content as so:
location / {
root /opt/production/app1/public;
}
Is this at all possible? If not (which I suspect is the case), is there a way to work around this w/o changing the url schema?
If I understand your question correctly, you can try an alias directive for /apps/app1/ location:
location /apps/app1/ {
alias /opt/production/app1/public/;
}
I have to create a server_name as a listener for origin pulls by my CDN.
The CDN wants to pull from origin.mydomain.com
I already have 100s of lines of code under www.mydomain.com that showcases all the rewrites, rules and such, and I need to use all this code again.
My easy solution would be to have
server_name www.mydomain.com origin.mydomain.com
To easily have NGINX listen for the requests to the "origin" subdomain.
My fear is that google discovers the subdomain and starts crawling it. I'd like to block google from the "origin" subdomain somehow. Since declaring multiple server_name, I am not sure I can just place robots.txt file somewhere, since using same root folder as live site.
Is there an easy way to do this?
All feedback appreciated.
Cheers
Ryan
Use two server blocks and use the include directive to pull in the common code. For example:
server {
server_name www.mydomain.com;
include /path/to/common/config;
location = /robots.txt {
root /path/to/friendly/dir;
}
}
server {
server_name origin.mydomain.com;
include /path/to/common/config;
location = /robots.txt {
root /path/to/unfriendly/dir;
}
}
So you have two robot.txt files in different directories - or use rewrite ... last to map the URI to different local files.
I am running Play framework server behind nginx server. At the root path, I am serving static website and all other paths should be redirected to the Play server. I have the following default.conf file in /etc/nginx/conf.d (The system is RHEL 6.7)
# to match the root path only to serve static website
location = / {
root /usr/share/nginx/html;
index index.html index.htm;
# try_files $uri $uri.html $uri/ /index.html;
}
# to match the cms login page
location /cms/ {
proxy_pass http://localhost:9000/;
}
# to match all the requests from the cms
location / {
proxy_pass http://localhost:9000/;
}
However, this configuration doesn't match the root path request. It gives 404 error. However, if I remove the third location rule, then it serves the static page at the root path.
Also, I noticed that first time I tried this, it worked. But now, it's not working any more. Please help.
The result you are getting is most likely due to the 2nd and 3rd location blocks not having "index" directives set. Except for well understood specific reasons, such as overriding the default index file type(s), the "index" should always be set at least within the server context or, preferably, within the http context. Similarly, the "root" directive should be set in the server context.
With your config, when a request hits the 3rd location block, there is no information your what to do with it. Actually, the 2nd block should not be needed from what you have described.
Also, as you are proxying to what appears to be another webserver, you need to ensure that this has the equivalent of "index" and "root" set.
Not sure exactly how the backend you are using works with respect to these. If not configurable there, then you must ensure that that every request hitting it has the URI spelt out fully.
To start with, depending on how exactly things are set up on your server, I will move the "index" and "root" directives up to the "server" level