Why is my nginx reverse proxy not working? (macos) - macos

I installed nginx using brew install nginx
Screenshot of the terminal commands and their results
I started nginx using brew services start nginx
I changed the usr/local/etc/nginx/nginx.conf file to add a new location and i changed the user to mobi staff
I added
location /p1/ {
proxy_pass http://127.0.0.1:5000/api/product;
}
location /p2/ {
proxy_pass http://www.google.com;
}
Neither of the proxy pass things are working, i added the google.com one to test if it would work but when i go to localhost:8080/p1/ or localhost:8080/p2/ i get a 404 not found. How do I fix this so I can setup a reverse proxy that will go to those links?
Screen shot of the install and nginx.conf

I figured it out, I moved the
location /p1/ {
proxy_pass http://127.0.0.1:5000/api/product;
}
location /p2/ {
proxy_pass http://www.google.com;
}
code to be just below the server
server {
listen 8080;
server_name localhost;
location /p1/ {
proxy_pass http://127.0.0.1:5000/api/product;
}
location /p2/ {
proxy_pass http://www.google.com;
}
and then instead of running
sudo brew services restart nginx
I ran
brew services restart nginx
and it works i get redirected to the urls now!

Related

using nginx reverse proxy for docker container

I am using nginx on my ubuntu machine and setup 2 laravel application using docker and one wordpress website without docker
Application 1: localhost:8088
Application 2: localhost:8089
I wanted to achieve is that when someone open localhost so it opens wordpress website and if someone open localhost/app1 it opens application 1 and so on.
So I have created reverse proxy so that it can open my docker container application
This is what I have done
sudo nano /etc/nginx/sites-available/website
ln -s /etc/nginx/sites-available/website /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx
After doing so when I try to open localhost/app1 it shows 404 but it recognise its a laravel app but shows 404
Here is my /etc/nginx/sites-available/website file code
server{
listen 80;
server_name localhost;
root /var/www/html/wordpress;
location /app1/{
proxy_pass http://localhost:8088;
}
}
You can create a file named redirects.map inside the nginx folder of your application and add a mapping like
~^localhost/app1/(.*) localhost:8089/$1;
You should change nginx configure from
server{
listen 80;
server_name localhost;
root /var/www/html/wordpress;
location /app1/{
proxy_pass http://localhost:8088;
}
}
To
server{
listen 80;
server_name localhost;
root /var/www/html/wordpress;
location /app1 {
proxy_pass http://localhost:8088;
}
}

Nginx configuration setup for windows

I am trying to run my vue applications trough nginx on windows and i was using the following tutorials, one to run nginx with AlwaysUp and the other one to configure it.
https://www.coretechnologies.com/products/AlwaysUp/Apps/RunNginxAsAService.html
https://graspingtech.com/nginx-virtual-hosts-ubuntu-16-04/
I also stumbled upon the following stack overflow question which is basically the problem i have but it didnt work:
nginx Windows: setting up sites-available configs
The service is running and recognizes the two domains i am trying to set up but for whatever reason it always sends me back to the NGINX Welcome page and i am not sure what i am doing wrong.
I followed the steps on the second tutorial and did a few changes, such ass adding "server_names_hash_bucket_size 64;" to my nginx.config file. I also created the symlink between the "sites-available" and "sites-enabled directories" using windows mklink.
Here are my files.
Nginx.config
worker_processes 1;
events {
worker_connections 1024;
}
http {
server_names_hash_bucket_size 64;
include mime.types;
default_type application/octet-stream;
include "C:/nginx/nginx/sites-available/*.conf";
sendfile on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
My app config file in sites-available which also contains a symlink in sites-enabled:
server {
listen 80;
listen [::]:80;
server_name myapp.nginx.br;
root "C:/Users/Documents/git-repository/my-app/dist";
index index.html;
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
I also faced the issue how to kill the nginx process. I stumbled a while until I came up with the following command which works:
taskkill /F /FI "IMAGENAME eq nginx.exe"
Well, after many hours i actually found out what was happening.
Everything in my files, from the nginx.conf file to every other .conf files were ok.
However i discovered that when restarting or stop/starting my nginx service with either the AlwaysUp program or the windows service options the service would still be active somehow and wouldnt apply my changes, therefore it would always show me the "welcome to nginx" page.
So i just restarted my computer because i wasnt able to kill the service with conventional means and it worked just fine for every single app i have.
I am sure there is a better way to kill the service and restart it but restarting my computer so the changes to my files would actually be applied solved it.
EDIT: I also discovered that windows takes a little while to stop the nginx service, so if you are using always up try stopping the service there, if it fails try stopping the service trough windows services menu. Also don't forget to set it to manual so you wont accidentally access you nginx app instead of your actual deployed app.

Basic browser authentication with Safari Capybara Selenium

I've got a problem with browser authentication on Safari using Capybara/Selenium.
I'm using this code to authenticate:
visit "https://#{ENV['AUTH_USERNAME']}:#{ENV['AUTH_PASSWORD']}#my-staging-app.heroku.com"
This works just fine on Chrome and FF but not on Safari.
Any ideas how to bypass this?
Okey, I've found the solution for this. I had to use reversed proxy using e.g. Nginx and send proper headers :)
Here is how I've done it:
In this example I'll be using creds login: admin and password: secret123.
Go to https://www.base64encode.org and encode your creds admin:secret123.
In this example it's YWRtaW46c2VjcmV0MTIz
brew install nginx
sudo vim /usr/local/etc/nginx/nginx.conf
Past there this code:
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost;
location / {
proxy_pass https://your_app.herokuapp.com;
proxy_set_header Authorization "Basic YWRtaW46c2VjcmV0MTIz";
}
}
}
Change proxy_pass to match your app url.
And proxy_set_header to Authorization "Basic <your_encoded_creds>"
Then: brew services start nginx
From now on, when you'll hit http://localhost:8080 you'll be redirected to your page and logged in.

Multiple websites with one IP: settings with proxy server and proxy cache (flask, uwsgi, nginx)

I want to allow my nginx server to server multiple subdomains.
Each site is served by a flask + uwsgi, listening to own ports.
All sites have many endpoints with same name, and responses are cached in different zones: I want to serve the correct cache (or correct site) from the proxy server.
I read https://askubuntu.com/questions/766352/multiple-websites-on-nginx-one-ip : in my configuration I keep having domain2 redirected to domain1.
I cannot find proper configuration for listening to uwsgi and have the proxy server serve the correct site.
How to properly set the ports and proxy_cache on the proxy_server to allow nginx serve two flask sites from a single server?
Below is my set current setup:
configuration domain_1
server {
server_name www.domain1.com;
return 301 $scheme://domain1.com$request_uri;
}
server {
listen 8000 default_server;
server_name domain1.com;
root /var/www/example_site_1;
# common locations for all sites
location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/example_site_1/domain1.sock;
}
# API
location /api {
include uwsgi_params;
uwsgi_param UWSGI_SCRIPT wsgi;
uwsgi_pass unix:/var/www/example_site_1/domain1.sock;
}
}
# Set cache directory for site
proxy_cache_path /tmp/nginx/domain1 levels=1:2 keys_zone=my_zone_domain_1:10m max_size=50m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
# Virtualhost/server configuration
server {
listen 80 default_server;
server_name domain1;
root /var/www/domain1;
## how to serve proxy_cache if locations of domain_1 and domain_2 are the same ?
location / {
proxy_cache my_zone_domain_1;
add_header X-Proxy-Cache $upstream_cache_status;
include proxy_params;
proxy_pass http://domain1.com:8000;
}
location /api {
add_header X-Proxy-Cache $upstream_cache_status;
proxy_cache my_zone_domain_1;
proxy_pass http://domain1.com:8000/api;
}
}
configuration domain_2
server {
server_name www.domain2.com;
return 301 $scheme://domain2.com$request_uri;
}
server {
listen 3000;
server_name domain2.com;
root /var/www/example_site_2;
# common locations for all sites
location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/example_site_2/domain2.sock;
}
# API
location /api {
include uwsgi_params;
uwsgi_param UWSGI_SCRIPT wsgi;
uwsgi_pass unix:/var/www/example_site_2/domain2.sock;
}
}
# Set cache directory for site
proxy_cache_path /tmp/nginx/domain2 levels=1:2 keys_zone=my_zone_domain_2:10m max_size=50m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
# Virtualhost/server configuration
server {
# I tried listening on other ports than 80, but kept having redirects on domain_1
listen 80;
server_name domain2;
root /var/www/domain2;
## how to serve proxy_cache if locations of domain_1 and domain_2 are the same ?
location / {
proxy_cache my_zone_domain_2;
add_header X-Proxy-Cache $upstream_cache_status;
include proxy_params;
proxy_pass http://domain2.com:3000;
}
location /api {
add_header X-Proxy-Cache $upstream_cache_status;
proxy_cache my_zone_domain_2;
proxy_pass http://domain2.com:3000/api;
}
}
Your domain_2 configuration uses proxy_pass http://domain2.com:8000 but there is only domain_1 server listens on the port 8000 so it gets to serve requests directed to domain_2.
I also recommend you to rethink usage of proxy_pass in your configuration, it isn't really necessary.
I found the culprit was Uwsgi : it was loaded from usr/bin folder, and not from virtual environment folder of my app.
Maybe virtualenv folder was corrupted: when I tried to re-install uwsgi (pip install uwsgi), it kept saying requirements satisfied until I noticed that which uwsgi was not loaded from virtualenv.
I had to remove and reinstall virtualenv folder with uwsgi and python modules.
Now applications were running (answer to the question), but proxy_server configuration has to be further adjusted in my case.
The following may be useful for people making use of url_for() directives in flask: url_for() directive routes to an absolute path, which may conflict with the nginx proxy.
Example: nginx proxy server listens on domains2.com on port 80, proxy_pass /path location to http://domain2.com:3000; if flask is redirecting a route to /path (with url_for()), the resulting url will be http://domain2.com:3000/path (because it follows the port specified in absolute path) instead of http://domain2.com/path (the url of the proxy).
Add proxy_set_header Host $http_host; to /path location to allow nginx proxy follow the correct redirect of your flask application.

trouble getting a file from node.js using nginx reverse proxy

I have set up an nginx reverse proxy to node essentially using this set up reproduced below:
upstream nodejs {
server localhost:3000;
}
server {
listen 8080;
server_name localhost;
root ~/workspace/test/app;
location / {
try_files $uri $uri/ #nodejs;
}
location #nodejs {
proxy_redirect off;
proxy_http_version 1.1;
proxy_pass http://nodejs;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Now all my AJAX POST requests travel just fine to the node with this set up, but I am polling for files afterward that I cannot find when I make a clientside AJAX GET request to the node server (via this nginx proxy).
For example, for a clientside javascript request like .get('Users/myfile.txt') the browser will look for the file on localhost:8080 but won't find it because it's actually written to localhost:3000
http://localhost:8080/Users/myfile.txt // what the browser searches for
http://localhost:3000/Users/myfile.txt // where the file really is
How do I set up the proxy to navigate through to this file?
Okay, I got it working. The set up in the nginx.conf file posted above is just fine. This problem was never an nginx problem. The problem was in my index.js file over on the node server.
When I got nginx to serve all the static files, I commented out the following line from index.js
app.use(express.static('Users')); // please don't comment this out thank you
It took me a while to troubleshoot my way back to this as I was pretty wrapped up in understanding nginx. My thinking at the time was that if nginx is serving static files why would I need express to serve them? Without this line however, express won't serve any files at all obviously.
Now with express serving static files properly, nginx handles all static files from the web app and node handles all the files from the backend and all is good.
Thanks to Keenan Lawrence for the guidance and AR7 for the config!

Resources