I have a dropplet on digital-ocean where I have a node.js app and nginx.
So, I installed nginx and then in /etc/nginx/sites-available/ I created a new file called api where I have the following content:
server {
server_name api.my-website.com;
location / {
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $proxy_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:9000; // this is where the node app lives
}
}
After I create that I restared nginx but if I go to api.my-website.com I dont see anything (my api has a landing page on /), but if I go to my-website.com:9000 I see the landing that should be on api.my-website.com.
Can someone explain me please what I'm doing wrong?
You did not included your /etc/nginx/sites-available/api file into /etc/nginx/nginx.conf. You need to include /etc/nginx/sites-available/api in /etc/nginx/nginx.conf inside http module and then restart or reload nginx. Your /etc/nginx/nginx.conf may looks like
http {
# other lines for logging, gzip, etc.
# other include may here
# Your include as following
include /etc/nginx/sites-available/api;
}
Related
We built an Nginx Server for acting as Edge Proxy for all the ligth weight front-end websites built on different small VMware instances.
The idea is to access the remote server's website like so:
https://nginxserver.mydomain.com/dev/marketing/
And the remote server is configured as upstream in Nginx's config directories:
upstream marketing-dev {
zone marketing-dev-service 64k;
server someserver.mydomain.com:8443;
sticky cookie srv_id expires=1h;
}
And the remote server's dev-marketing.conf file looks like this:
location /dev/marketing/ {
access_log /var/log/nginx/marketing-dev-access.log main;
error_log /var/log/nginx/marketing-dev-debug.log debug;
proxy_pass https://marketing-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;
}
When I access the URL, site loads fine, and I can browse to almost all locations and pages within.
Except for few!!
It is dropping the location /dev/marketing/ in the URL and takes me directly to https://nginxserver.mydomain.com/ or takes me to locations by skipping the path.
For example, https://nginxserver.mydomain.com/aboutus instead of https://nginxserver.mydomain.com/dev/marketing/aboutus
Anyway to overcome this?
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 nginx, apache, and node setup on my CentOS server.
Node runs on port 8080.
In my default.conf nginx file, I have
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
That works. When going to my site's homepage (and any other page that node uses, such as /login and /signup page), I see my node app correctly.
I have a couple of other rules like this one, in order to let nginx serve the static files instead of node serving them.
location /javascripts {
root /var/www/myWebsite/public;
}
I am setting up a forum, and I'm using apache and php for it. The forum works at myWebsite.com:90, however I want to make the forum work without the port number.
Apache has Listen 90, and DocumentRoot is set to the forum path.
Nginx conf has this
location /forum {
proxy_pass http://127.0.0.1:90;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
But that doesn't work, I get a 404. If I go to some giberrish page, I get the node's error page, so we can see that something is working by getting the 'normal' web 404 instead of my custom node's 404.
How can I remove that :90 port now?
Thanks
upstream apache_server {
server 127.0.0.1:90;
keepalive 60;
}
server {
listen 80;
location / {
proxy_pass http://apache_server
}
}
I am new to nginx and I'm struggling to get my configuration for a reverse proxy working. I have a node app running on localhost:3010 and I'm trying to serve pages through nginx from this app at the subdomain dev.[sitename].org. Let's just say dev.example.org for readability. Here are the contents of a file I created in sites-available called example.org (is that the correct name for this file?):
server {
server_name www.example.org example.org;
}
upstream app_dev.example.org {
server 127.0.0.1:3010;
}
server {
listen 0.0.0.0:80;
server_name dev.example.org;
access_log /var/log/nginx/dev.example.access.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_dev.example.org/;
proxy_redirect off;
}
}
This is mostly based off this related question: Node.js + Nginx - What now? however when I try to open dev.example.org in my browser, Chrome reports that it can't find the page. I can ping dev.example.org and get an IP address, so the server seems to be available, but my nginx configuration incorrect. I created the symlink in sites-enabled and restarted nginx, in case you thought I might have forgotten those steps.
So my thought now is that I'm not referring to the subdomain correctly somewhere, or maybe my file in sites-available is named wrong. Any push in the right direction would be appreciated.
Just to be sure the problem is on nginx try these steps:
Set a test server at port 3030, serving the system doc folder or anything else.
server {
listen 3030
location / {
root /usr/share/doc/;
autoindex on;
}
}
upstream simple_test {
server 127.0.0.1:3030
}
Then use simple_test below as well:
proxy_pass http://simple_test/;
If you see the /usr/share/doc dir listing when you access dev.example.org then your issue is on the node side.
Turned out something was blocking port 80! fixed that and the config as posted above worked
I have a few applications that are running localy in defferents ports, how can I configure NGINX server for forwarding request from port 80 to my application depends on income domain name. For example 2 local apps named 'app1' on port 8181 , and if request comes from http://app1.com - nginx forwards to http://localhost:8181
I've looked at nginx docs, I ask for your examples if someone did this.
Thanks
Assuming you want to create a reverse proxy, my method is to first configure the following reverse proxy settings in a new file called /etc/nginx/reverse-proxy.conf:
# Serve / from local http server.
# Just add the following to individual vhost configs:
# proxy_pass http://localhost:3001/;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
Then, for each reverse proxy I'm configuring, I add an appropriately-named configuration file in /etc/nginx/sites-enabled with the following:
server {
server_name app1.com;
server_name www.app1.com;
location / {
include /etc/nginx/reverse-proxy.conf;
proxy_pass http://localhost:8181/;
}
}
You can create as many server blocks as you like and point them at different local (or even remote) application servers. You can also add location blocks to serve different URLs within the same domain statically, or from different local application servers.
(You can also just roll all the configuration into /etc/nginx/nginx.conf, but I find it easier to separate configuration out into multiple files.)
I managed to do this easily by following this tutorial.
Create a new file in /etc/nginx/conf.d/ called your-domain.com.conf and put this in it:
server {
listen 80;
server_name your-domain.conf.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2368;
}
}
Then restart nginx
sudo service nginx restart