I'm new to NGINX (well hosting in general), so please excuse my ignorance. I have a Phoenix web app running on localhost:4000 and an ASP.NET web app running on localhost:5123. I'm trying to utilize NGINX to create a reverse proxy so that either web app can be accessed from the same domain. My NGINX config file contains the following:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:4000;
}
location /test {
rewrite /test(.*) /$1 break;
proxy_pass http://localhost:5123;
}
}
I'm able to access server 1 on example.com/ and server 2 on example.com/test, however, server 2 is not able to load its own css, javascript and image files. Is there a way to ensure server 1 and server 2 utilize their own resources via the NGINX config setup?
You are on the right way. There are some lines missing on your second service:
Replace...
location /test {
rewrite /test(.*) /$1 break;
proxy_pass http://localhost:5123;
}
with...
location /test {
rewrite /test/(.*) /$1 break;
rewrite ^/test$ /test/ permanent;
proxy_pass http://localhost:5123/;
proxy_redirect / /test/;
proxy_set_header Host $host;
proxy_buffering off;
}
I don't know if this is the best way to achieve this. Long time ago I used this configuration to implement an etherpad-service on my server. That time I had not the option to use a subdomain. Anyway - using a subdomain for your second service would be better.
Related
I'm trying to rewrite a specific request url using nginx, but I've only gotten so far as either a permanent redirect loop or no rewrite at all.
The rewrite rule I'm trying to achieve is the following:
FROM https://localhost/api/economic/grantaccess?id=123&token=foobar
TO https://localhost/customer-api/api/economic/grantaccess?id=123&token=foobar
I'm just looking to add /customer-api/ to this specific request, any other requests should be picked up by the /customer-api/ location. Meaning that after the redirect is made to https://localhost/customer-api/api/economic/grantaccess?id=123&token=foobar the location that should be used to serve that request should be /customer-api/ so that the proxy_pass kicks in and connects to the backend.
Here is my simplified nginx config:
http {
default_type application/octet-stream;
server_tokens off;
upstream customerapi {
zone customerapi 64k;
server 127.0.0.1:6564;
}
server {
listen 80;
server_name localhost;
root /apps;
index index.html;
location ~ /api/economic/grantaccess(.*)$ {ยด
rewrite ^ /customer-api/api/economic/grantaccess$1$is_args$args break;
}
location /customer-api/ {
rewrite ^ $request_uri;
rewrite ^/customer-api/(.*) $1 break;
return 400;
proxy_pass http://customerapi/$uri;
proxy_set_header Host $host;
}
}
}
When using break flag the rewrite does not work at all because nginx tries to open the new path as a file in the filesystem instead of picking up the request via the /customer-api/ location, and if I use permanent flag I get stuck in a permanent redirect loop.
Any help is very much appreciated.
I'm trying to run a django app and and angular one on my VPS using Nginx. Below is my config file code:
server {
listen 80;
server_name www.the-patron.com the-patron.com;
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location /staticfiles/ {
root /root/thepatron/The-Patron-Backend;
}
# Django Backend
location /back/ {
include proxy_params;
proxy_pass http://unix:/root/thepatron/The-Patron-Backend/thepatron.sock;
}
# Angular Frontend
location / {
proxy_pass http://localhost:4200/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
}
Here my Angular app is running well and the Django one isn't. If I change location /backend/ to location / on line 12 and location / to location /frontend/ on line 18 then I will get the Django app to run while the Angular app will not.
How can I run both and change the location of each?
As my previous approach to host my Angular and Django apps separately was totally wrong. I finally managed to solve my issue thanks to the comments of #Vipulw above.
What worked for me is that I built my Angular application to production and placed the generated build folder inside my static files folder in my Django app directory, and then configured my Nginx to serve that Angular build.
Below is my new Nginx config file:
# Angular Reverse Proxy
server {
listen 80;
server_name <Domain name or IP address>;
root /root/<path to Django app>/static/<Angular app build folder>;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
# Django Reverse Proxy
server {
listen 8080;
server_name <Domain name or IP address>;
location / {
include proxy_params;
proxy_pass http://unix:/root/<path to Django app>/<myapp>.sock;
}
}
Additional Note:
In this way mentioned above, my Angular app is running on port 80 while my Django app is running on port 8080. Everything is working fine this way but I don't see that as the best way to do run both Angular and Django. After a little bit of research, I found that the best way to serve Angular and Django in one app is to let the default Django route to point to the Angular build. The default Django route right now is pointing to the Django Rest Framework root directory.
Sadly, I don't know how to let Django's default route to point to the Angular build file at the moment.
I am new to Nginx and I currently trying to deploy my web app on my server.
I have static files (built from react) being served by Nginx. The static files make calls to port 5000, my flask server. When testing, flask cannot receive any calls from my static files.
The (static-flask) setup runs on my local machine, so I am assuming that there is a problem with my config with Nginx.
Here is my Nginx setup (in sites-enabled):
server {
listen 80;
server_name MY_IP_ADDRESS;
location / {
root MY_LOCATION_TO_STATIC_FILES;
index index.html;
try_files $uri /index.html;
}
}
I am guessing that once Nginx serves the static files, the client (static files) make calls to localhost:5000, but does not refer to port 5000 on my server?
How would I serve the static files such that they can refer to the server's localhost:5000?
Edit
I guess I should be more specific with my project. I want to serve my static files when the user hits www.mydomain.com, and when the user interacts with the website, they make calls to a flask server running on port 5000 on my server.
I could consider serving static files from flask, but that would be highly inefficient.
Use nginx proxy:
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:5000;
}
Try this
server {
server_name www.yourdomain.com;
location /static {
alias /home/user/path/static;
}
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
}
Change alias to your path project
I would like to run 2 jenkins server behind nginx reverse proxy, but I can not find the proper to configure it.
The config below is working fine
location /jenkins {
proxy_pass https://contoso.com/jenkins;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
If i try to change location to /jenkins_test, than it does not work anymore.
What do I wrong?
You will need two define each jenkins instance in its own server section.
Then depending on the url that you are calling on nginx, the right jenkins server will respond.
Your nginx config could have a structure like this:
http{
# application server for first jenkins instance
upstream app_servers_first_jenkins_instance {
# if jenkins is running on the same server this should be something like 127.0.0.1 ...
server https://contoso.com/jenkins;
}
# application server for secons jenkins instance
upstream app_servers_second_jenkins_instance {
server https://contoso.com/jenkins;
}
# JENKINS SERVER 1
server{
listen 80;
server_name jenkinsfirstinstance.yourdomain.com;
location / {
proxy_pass http://app_servers_first_jenkins_instance;
}
}
# JENKINS SERVER 2
server{
listen 80;
server_name jenkinssecondinstance.yourdomain.com;
location / {
proxy_pass http://app_servers_second_jenkins_instance;
}
}
} # END OF HTTP SECTION
In this example both urls will call the same jenknins endpoint (https://contoso.com/jenkins) if you want them to be different jenkins instances you will have modify this url in one of the upstream sections
If you want to run 2 servers behind the nginx proxy, that's mean you need 2 location contexts (also called "blocks").
In your configuration file which is probably located in /etc/nginx/sites-availables you should add the locations:
http{
listen 80;
location /jenkins1 {
proxy_pass http://jenkins1-local-ip-address:8000;
include /etc/nginx/proxy_params;
}
location /jenkins2 {
proxy_pass http://jenkins2-local-ip-address:8001;
include /etc/nginx/proxy_params;
}
}
One thing you schould note is that I consider that your jenkins server is in same LAN (Local Area Network) otherwise it will not make sense to habe a proxy in front because your sever is already accessible via internet.
If your jenkins servers are accessible via HTTPS you schould change http to https in a location context and edit the port number to listen 443 and some ssl certificates configurations.
I curently have a Forefront TMG2010 doing reverse proxy to several internal web servers. Since it was really easy in TMG I have done lots of host and virtual directory mappings/rewrites.
Now I ened to acheive the same in NGINX.
e.g.,
apphost.domain.com/ needs to map internally to server1/apppath/
apphost.domain.com/test/ needs to map internally to server2/apppath/
apphost.domain.com/dev/ needs to map internally to server3/apppath/
in each case the internal /apppath/ needs to be invisible to the client. This is to achieve the same as the external to internal path mapping in TMG.
What I have done so far is;
server {
listen 80;
server_name host.server.com;
location /test/ {
proxy_set_header Host $host;
rewrite ^(.*)$ /app/$1 break;
proxy_pass http://10.0.0.2;
}
location / {
proxy_set_header Host $host;
rewrite ^(.*)$ /app/$1 break;
proxy_pass http://10.0.0.1;
}
But only the / seems to work, the /test/ doesnt.
Help!
So you want to map /test/foo to http://10.0.0.2/app/foo. The simplest solution is:
location /test/ {
proxy_set_header Host $host;
proxy_pass http://10.0.0.2/app/;
}
According to the documentation the /test/ element will be replaced by /app/ as it passes upstream.