So i have a strange configuration that i need until i get to refactor a project later this year.
it is a static index.html landing page served with a location root "/" by NGINX.
i have a slash location like so:
location /channel/ {
proxy_pass http://node_server:5000/;
proxy_redirect off;
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-Ssl on;
proxy_cache_bypass $http_upgrade;
}
that express.js then handles. Express just serves a static folder with auto-generated content that a user can then navigate around in.
It works well in the sense that any appending slashes like:
/channel/open/
but ONLY if i have a trailing slash after "open". If i leave it out then NGINX will throw a "404 Not Found", if i keep it in, it works as intended.
The thing is that a user should be able to click on a folder in the static served directory to traverse a file structure, and the way that works in a browser is that it does not append a trailing slash to a directory name when clicked, so NGINX then throws the "404".I have tried a NPM package called 'express-slash' but that did not solve the issue.
i was hoping someone here had some suggestions on what i could try out next?
Details about trailing slash behavior can be found here: https://serverfault.com/questions/607615/using-trailing-slashes-in-nginx-configuration
But if your goal is to configure NGINX to allow directory traversal, you may be looking for the autoindex directive: http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
Related
location/merchant/mangooda
rewrite /merchant/mangooda / break;
proxy_redirect off;
proxy_set_header Host $host;
proxy_pass http://localhost:9001;
}
This is my nginx configuration. I want to get the localhost:9001 when I go to the server_name/merchant/mangooda. But now when I go to the 'server_name/merchat/mangooda', it's going to the localhost:9001/merchant/mangooda. This path does not exist in localhost:9001. So, I got 404 error. Can anyone fix my problem?
In case 'proxy_pass http://localhost:9001/', when I change the proxy_pass like this, url is working but it doesn't load CSS contents in my NodeJS app. only showing HTML pages.
i want to host two small websites, both made with strapi backend and react frontend, on my server which is a digital ocean droplet.
I already configured nginx in order to work for one of the websites and everything is working correctly. I can access strapi from site1.com/dashboard and my queries point to site1.com/api/graphql. I followed some tutorials for that.
Here are the nginx files i added:
/etc/nginx/sites-available/site1.com:
server {
listen 80;
listen [::]:80;
root /var/www/site1.com/react;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name site1.com www.site1.com;
location /api/ {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://strapi;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $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_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass_request_headers on;
}
location /dashboard {
proxy_pass http://strapi/dashboard;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $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_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass_request_headers on;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
}
}
file /etc/nginx/conf.d/upstream.conf:
upstream strapi {
server 127.0.0.1:1337;
}
What i now want to do is to deploy another website to the same server and configure nginx to serve it as well (on another domain, for example site2.com).
So i added the nginx virtual server file for the second website, copying the first one and changing the domain name and root directory.
The site 2 frontend is now working correctly and accessible on its domain.
However when i start the site 2 strapi instance it says the port 1337 is already in use (obviously, it is used by site 1 strapi instance that is running with pm2). So i changed the port in strapi config to be 1338 (is it ok?) but now i don't know what do to in nginx in order to serve the two different strapi instances on different domains.
The hostname you are using for the proxy_pass directive is the upstream name you defined in the separate config file. This is basically an indirection to one or more real backends. In your case that's the application running on port 1337 on the same machine. It could also be a list of external domains where nginx takes care to distribute the load to.
Your approach with additional virtual hosts already looks good. The frontend that already "works" under site2 is probably the old instance served under the new domain if your proxy_pass directive still points to http://strapi for site2 (which probably still resolves to `localhost:1337).
As you already mentioned, a second instance of the backend needs to run on a different port. The port number you use is not really important as you will control this with your upstream configuration. Just make sure to not use a port number below 1024 (which requires root permissions), don't conflict with other processes on the system (you will notice) and as best practice, don't use port numbers that are default for some other protocols (maybe check this list).
To fix your setup, just define a second upstream like the first one but pointing to the new url e.g. localhost:1338 and then reference this new name in the proxy_pass directive of site2.
Technically, with just one backend per upstream, you could also skip the upstream part completely and reference the URL directly in the proxy_pass directives, but using a shorthand name can also support readability of your configuration.
I have a NodeJS + ExpressJS app running on port 3000 which has a route:
router.get('/getHello', function(req, res) {
res.send('Hello, world!');
});
and a HTML page which does a GET on this route
<a href="/getHello">
<input type="button" value="Visit Helo World page" />
</a>
This standalone app works as intended. It displays Hello, world! when the button is pressed.
Now, here's the problem:
I have setup a reverse proxy on this app using nginx. Here's the config file from sites-available (linked with sites-enabled)
server {
listen 80;
server_name localhost;
location /routetest/ {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
proxy_cache_bypass $http_upgrade;
}
}
The app opens fine when I visit http://localhost/routetest but when I click on the button, it opens http://localhost/getHello whereas I wanted it to open http://localhost/routetest/getHello. Since http://localhost/getHello is not defined, I end up with a 404. (On the other hand, http://localhost/routetest/getHello correctly returns the Hello, world message)
Now, my question is:
My frontend is written with / as the base path (to make all GET and POST requests), for example <a href="/getHello"> and I feel manually appending /routetest/ before all URLs in my HTML not a good practice (since I may want to change this base path later and then I'll have to update it everywhere). So, is there a way for NGINX or some express middleware to add /routetest/ for URLs that are written with / as the base path, without having to change it in my HTML manually?
By a long way the easiest solution is to use relative paths without the leading / if that's an option that's available to you.
Rewriting URLs in the response is somewhere between tricky and impossible in the general case. If you think you can successfully identify the relevant URLs in your content you might be able to use nginx's catchily titled ngx_http_sub_module:
http://nginx.org/en/docs/http/ngx_http_sub_module.html
There are various Express middleware implementations that do a similar thing but I couldn't find one that looked reliable enough to recommend. The basic approach would be similar to the compression middleware if you fancy giving it a go yourself. Again it's quite difficult in general but depending on the specifics of how you serve up your HTML it might not be too bad.
I need some help with this one, because I've been stuck on it for far too long. Here's my use case:
I need to proxy from my main domain test.co to two separate locations, at various endpoints. For now, however, I simply want to use location blocks for = / (homepage) and / (everywhere else). The two separate locations are webflow.test.co at the homepage (real domain removed for anonymity), a subdomain of ours, and our NodeJS server at everywhere else. Here's the problem:
Using the code below, (my nginx.conf, ran through an ElasticBeanstalk instance) results in an infinite redirect loop at the homepage /. The error in the browser is ERR_TOO_MANY_REDIRECTS, but it's clear what's happening: the browser is redirecting from the original test.co to https://test.co, to https://test.co again ad nasueum. I've tried changing nearly everything I could think of... some examples: the server_name was originally a regex, I removed that. The / location block was once a regex, had some issues with that, I removed it. Added X-Forwarded-Proto $scheme to the headers.
Those are just a few things, but here's the bottom line: the only thing that seems to work, through 3 days of testing, is removing proxy_set_header Host $host;, or setting it to webflow.test.co (thus defeating the purpose). This is an unacceptable solution, of course, because I need the URL to match my original domain... but it does solve the problem. Perhaps that can give you guys some insight.
Here's my code:
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
# Redirect all non-HTTPS to non-WWW HTTPS
server {
listen 8080;
server_name "~^(?:www\.)?(.*)$";
return 301 https://$host$request_uri;
}
# Redirect WWW HTTP to non-WWW HTTP
server {
listen 4430;
server_name "~^www\.(.*)$";
return 301 https://$1$request_uri;
}
# Reverse-proxy to http://nodejs
server {
listen 4430;
server_name test.co;
client_max_body_size 50M;
location = / {
proxy_pass http://webflow.test.co;
proxy_set_header Connection "";
proxy_http_version 1.1;
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 / {
proxy_pass http://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Thanks so much to anyone who takes the time to solve this one. It's been a thorn in my side, and I've gotta move on to something fresh. Will accept the first answer that works immediately.
Some other notes - may be important - that I'll add to if things get uncovered:
curl -v test.co returns a 301 with a Location of https://test.co.
curl -v https://test.co returns the same - 301 with a Location of
https://test.co
Removing the two non-WWW and non-HTTPS at the top doesn't solve the
problem. It makes test.co unreachable, but https://test.co still
redirect loops
I've recently installed GitLab, and I'm using the bundled NGINX with it. I've been able to successfully set it up, and put it on mydomain.com/lab
Great! Only problem is that when you go to mydomain.com, it has a 404 page. Well, duh. I need to upload my index.html... but WHERE do I do it?
How can I add my index page to the bundled NGINX? Most of all, WHERE do I upload it?
I'm using CentOS 7, and for the love of God I swear I've looked in every directory and I have no idea where to throw my index page in. Please bear with my ignorance as I'm quite new to this.
It seems you have install GitLab under a relative url (subdomain), using the relative_url_root rule in gitlab.yml.
You can try in your gitlab nginx config file, to define a root and redirect the rest to another location, in which the gitlab reverse_proxy rules apply: see "Getting Nginx to point to a specific index.html"
location / {
root /custom path/for/index.html;
error_page 404 = #gitlab;
expires 30d;
}
location #gitlab {
client_max_body_size 0;
gzip off;
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Host $http_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 http://gitlab-workhorse;
}