Nginx location app urls - docker

I'm trying to get routing within the nignx config working. I have an app at http://app1:8081 and another app at http://app2:8080. (FYI I'm using docker containers so each app is in its own container) What I have working is for nginx is app1 is point to http://example.com. Where I'm having trouble getting http://example.com/gc to work.
server {
listen 80;
server_name http://example.com;
location /gc/ {
proxy_pass http://app2:8080/;
}
location / {
proxy_pass http://app1:8081/;
}
}
I've tried the proxy_pass with and without trailing / and the location with and without trailing /. I've had an odd result where going to example.com/gc/ would rewrite to example.com/home which didn't work.
I'm was hoping for something that is similar to IIS with application folders under a site. If you have a site that points to example.com and put an application named gc and point it to the application folder.
The end result should be example.com/gc/home renders app2:8080/home.
Any help with my nginx config would be greatly appreciated.

Related

Nginx returning 404 when trying get subroute in dockerized react app

I have a react app served with serve on docker.
I use Nginx to rewrite requests to my app with the following config:
http {
upstream myapp_servers {
server 1.2.3.4:8000;
}
}
server {
listen 80;
server_name myapp.com;
location / {
proxy_pass http://myapp_servers;
}
}
When I access my app with myapp.com it works fine.
If i want to access a subroute on my app like myapp.com/route nginx returns 404 error,
try_files $uri index.html only works if nginx serves static files.
How can i solve this issue for dockerized react app?
Ok so I figured it out, the issue was not with Nginx.
What happened is that I used vercel\serve for my deployment server and i had to run it in single mode with -s, also i had to remove homepage: "."
from my package.json and now the application works properly in production.

How do I map a location to an upstream server in Nginx?

I've got several Docker containers acting as web servers on a bridge network. I want to use Nginx as a proxy that exposes a service (web) outside the bridge network and embeds content from other services (i.e. wiki) using server side includes.
Long story short, I'm trying to use the configuration below, but my locations aren't working properly. The / location works fine, but when I add another location (e.g. /wiki) or change / to something more specific (e.g. /web) I get a message from Nginx saying that it "Can't get /wiki" or "Can't get /web" respectively:
events {
worker_connections 1024;
}
http {
upstream wiki {
server wiki:3000;
}
upstream web {
server web:3000;
}
server {
ssi on;
location = /wiki {
proxy_pass http://wiki;
}
location = / {
proxy_pass http://web;
}
}
}
I've attached to the Nginx container and validated that I can reach the other containers using CURL- they appear to be working properly.
I've also read the Nginx pitfalls and know that using hostnames (wiki, web) isn't ideal, but I don't know the IP addresses ahead of time and have tried to counter any DNS issues by telling docker-compose that the nginx container depends on web and wiki.
Any ideas?
You must turn proxy_pass http://wiki; to proxy_pass http://wiki/;.
As I know, Nginx would take two different way with/without backslash at the end of uri. You may find more details about proxy_pass directive on nginx.org.
In your case, a backslash(/) is essential as a uri to be passed to server. You've already got error message "Can't get /wiki". In fact, this error message means that there is no /wiki in server wiki:3000, not in Nginx scope.
Getting better knowing about proxy_pass directive with/without uri would help you much.
I hope this would help.

serving web app and python using nginx on remote server

Setup:
1> Web GUI using Angular JS hoasted in tomcat server and Python app using Flask is running on an AWS server.
2> I am working in a secure server hence am unable to access AWS directly.
3> I have setup NGINX to access GUI app from my local secured network. GUI app is running on awsserver:9506/appName
4> Flask app is running in AWS server hosted on 127.0.0.1:5000. This app has 2 uri's cross and accross:
127.0.0.1:5000/cross
127.0.0.1:5000/accross
Now in my GUI after NGINX setup i am able to access it using domain name and without port:
doman.name/appName
Now when i try to use it send a request to server my url changes to:
doman.name/cross. I did the changes in NGINX config and am able to access it but am not able to get a response back. Please find below my NGINX config file:
server {
listen 80;
server_name domain.name;
root /home/Tomcat/webapps/appName;
location / {
proxy_pass http://hostIP:9505/; #runs the tomcat home page
}
location /appName/ {
proxy_pass http://hostIP:9505/appName; #runs the application home page
}
location /cross/ {
proxy_pass http://127.0.0.1:5000/cross; #hits the python flask app and am trying to send post
}
}
Also what i noticed is that my POST request is being converted to GET at the server by NGINX
You need to be consistent with your use of the trailing /. With the proxy_pass statement (as with alias) nginx performs text substitution to form the rewritten URI.
Is the URI of the API /cross or /cross/? POST is converted to GET when the server is forced to perform a redirect (for example, to append a /).
Specifying the same URI on the location and proxy_pass is unnecessary as no changes are made.
If the hostIP in your first two location blocks is the same, and assuming that the missing trailing / is accidental, they can be combined into a single location block.
For example:
location / {
proxy_pass http://hostIP:9505;
}
location /cross {
proxy_pass http://127.0.0.1:5000;
}
See this document for more.

How can I host my API and web app on the same domain?

I have a Rails API and a web app(using express), completely separate and independent from each other. What I want to know is, do I have to deploy them separately? If I do, how can I make it so that my api is in mysite.com/api and the web app in mysite.com/
I've seen many projects that do it that way, even have the api and the app in separate repos.
Usually you don't expose such web applications directly to clients. Instead you use a proxy server, that forwards all incoming requests to the node or rails server.
nginx is a popular choice for that. The beginners guide even contains a very similar example to what you're trying to do.
You could achieve what you want with a config similar to this:
server {
location /api/ {
proxy_pass http://localhost:8000;
}
location / {
proxy_pass http://localhost:3000;
}
}
This is assuming your API runs locally on port 8000 and your express app on port 3000. Also this is not a full configuration file - this needs to be loaded in or added to the http block. Start with the default config of your distro.
When there are multiple location entries nginx chooses the most specific one. You could even add further entries, e.g. to serve static content.
While Svens answer is completely correct for the question given. I'd prefer doing it at the DNS level so that I can change the server to a new location just in case my API or Web App experience heavy load. This helps us to run our APIs without affecting WebApp and vice-versa
DNS Structure
api.mysite.com => 9.9.9.9 // public IP address of my server
www.mysite.com = > 9.9.9.9 // public IP address of my server
Since now you'd want both your WebApp and API to run on the same server, you can use nginx to forward requests appropriately.
server {
listen 80;
server_name api.mysite.com;
# ..
# Removed for simplicity
# ..
location / {
proxy_pass http://localhost:3000;
}
}
server {
listen 80;
server_name www.mysite.com;
# ..
# Removed for simplicity
# ..
location / {
proxy_pass http://localhost:8000;
}
}
Any time in future if you are experiencing overwhelming traffic, you can just alter the DNS to point to a new server and you'd be good.

Hosting Multiple websites on nginx similar to apache

I am trying to host multiple websites in nginx similar to apache. In apache we have folder called htdocs. We put directories and files inside htdocs and if the URL is something like
www.example.com
then we can access the directories and files inside the htdocs folder as
www.example.com/exampledir1 and www.example.com/exampledir2
I want to do the same with nginx. Is it possible. I have a server in my company which has a static IP. Ive installed nginx on it and by default i have hosted one rails app on it with the following lines in the nginx.conf file
server {
listen 80;
server_name mycomp.does-it.net;
root /var/www/mysamplerails/public;
passenger_enabled on;
rails_env development;
}
This site is accessible to me if i go to the url "mycomp.does-it.net"
now what i would like to do is have another rails app and access it from mycomp.does-it.net/mysecondrailsapp as the root.
Ive googled a lot and im ending up no where. I'm from apache background and these are some of my first tries in nginx.
Use passenger_base_uri option. See documentation: Deploying to a sub URI

Resources