Reverse proxy a URI to a Vagrant box - nginx

I have a Wordpress Vagrant box that I access through the URL localhost:9001 (forwarded port.).
I am currently trying to make it accessible through the URL "molecare.dev".
I have created the line in the hosts file that catches that URL and points it to my localhost (this is working because I can see the nGinx splash page) but I am having trouble catching this URL in the server block and proxy_pass'ing this to the URL(localhost:9001).
Here is my /etc/nginx/sites-available/default file`server_name molecare.dev;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
I have written the following code which I think should do it but I don't know where to place it and if it is correct?
server {
listen 80;
server_name molecare.dev;
location / {
proxy_pass localhost:9001;
}
}
Can anyone see if this is correct and if so where I put this?
Thanks!

In the folder /etc/nginx/sites-available/ create a config file for your site, say 'molecare.dev.conf'.
Modify the block you wrote to the following and put that in the new file and save it:
server {
listen 80;
server_name molecare.dev;
location / {
proxy_pass http://127.0.0.1:9001;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Then create a symlink:
ln -s /etc/nginx/sites-available/molecare.dev.conf /etc/nginx/sites-enabled/molecare.dev.conf
Reload the nginx configuration:
service nginx reload

Related

How to remove trailing slash after file extension using Nginx?

I have a Ghost site at example.com and www.example.com (internally, it's running on port 2368). At the same time, I have a file named form.pdf and I want to make it available (view in browser, not to ask user to download) at:
example.com/form.pdf
www.example.com/form.pdf
With configuration below, it only works if it contains trailing slash after the .pdf extension like so:
example.com/form.pdf/
www.example.com/form.pdf/
PDF file path:
/var/www/ghost/pdfs/form.pdf
Config file path:
/etc/nginx/sites-available/ghost
Config file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name 123.45.678.901 localhost example.com *.example.com;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 100M;
location "/form.pdf" {
alias /var/www/ghost/pdfs;
index form.pdf;
}
location / {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
When I remove the trailing slash after the .pdf extension, I got 404 error.
Question:
How to correctly configure Nginx to make it work without the trailing slash at the end?
I found the answer. This one worked for me:
location = /form.pdf {
root /var/www/ghost/pdfs;
}

Failed to load js/css from bitbucket server reverse peroxided by Nginx

I am very new with nginx and bitbucket server setup. I have an nginx server setup in front of bitbucket instance (running on port 7990). Below is my configuration of /etc/nginx/sites-available/default (not /etc/nginx/nginx.conf)
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /bitbucket/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:7990/;
sendfile off;
expires 0;
add_header Cache-Control private;
add_header Cache-Control no-store;
add_header Cache-Control no-cache;
}
}
Now if I hit
http://my_ip/bitbucket
it forwards to
http://my_ip/bitbucket/setup
page, but fails to load css and js files.
When I see the html source of setup page, it includes some js/css files' absolute paths while importing (e.g. link type="text/css" rel="stylesheet" href="/s/277823dc49fd32854c324f87e345f7f6-CDN/-196139424/aef68c7/1/1b615eee93091f36273c8450f7d72556/_/download/contextbatch/css/_super/batch.css"). This seems like nginx is looking for the css files in "s" folder under its own directory.
I also checked that
http://my_ip/bitbucket/s/277823dc49fd32854c324f87e345f7f6-CDN/-196139424/aef68c7/1/1b615eee93091f36273c8450f7d72556/_/download/contextbatch/css/_super/batch.css
Url returns the page in browser.
Please let me know how to load css/js files. I also want to know why the css url is so ugly(it seems that it includes some random numbers).

Nginx sub-path shows 404 error

I am trying to setup nginx to work with a subpath but I am receiving 404 error.I am using default location and html file.
Nginx config is the following:
server {
listen 80;
listen[::]:80 ipv6only=on;
server_name localhost;
## serving gogs
location / {
proxy_pass http://localhost:3000;
}
## serving laravel-based web app
location /yt/ {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
The firewall (ufw) is disabled and all ports are open via mikrotik.Everything is fine when open the / path but when I go to /yt/ I get a 404 error.
Not sure if configuration is correct, when i compared with my local simple setup i found it little different . Please try this one if it works.
server {
listen 80;
listen[::]:80 ipv6only=on;
server_name localhost;
## serving gogs
location /yt/ {
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_set_header Host $host;
}
}

Django Nginx Browser Caching Works Half-way

I have this in my file in my /etc/nginx/sites-available/
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/portforlio;
index index.html index.htm;
client_max_body_size 4G;
server_name khophi.co www.khophi.co;
keepalive_timeout 5;
location /media {
alias /home/portfolio/media;
}
location /static {
alias /home/portfolio/static;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
With the configuration above, I'm told I've leveraged files caching when I check via https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fkhophi.co%2F&tab=desktop
Yet, when I visit the site, it doesn't load the css and I get an unstyled page.
What is missing? I have tried this and that, yet although I have also specified my root directory, it still doesn't show them when requested in the browser.
Mysteriously, pageinsights sees them and even as cached, how?
the live site is at khophi.co
The answer I figured out to be a typo.
Should be portfolio not portforlio
I added an error log to nginx and from there, I noticed it says the directory didn't exist, that prompted me to check the folder names
error_log /home/nginx/nginx_error.log warn;

Why is Nginx routing all traffic to one subdomain?

I am new to nginx. I am trying to install GitLabs alongside an existing php project which is currently being served by Apache on port 80. My plan is to get them both working side by side on port 90 and then turn off Apache, switching both projects to Nginx on port 80.
Okay. The problem is that both subdomains are being captured by the server for my php project which should only be served to requests for db.mydomain.com. For the php project I have a file called: ccdb symlinked into /etc/nginx/sites-enabled. It contains:
server {
server_name db.mydomain.com;
listen 90; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/ccdb;
index index.html index.htm index.php;
}
However, for some reason, traffic to git.mydomain.com is being serverd from /var/www/ccdb even though I have another file symlinked alongside that one called gitlab with this content:
# GITLAB
# Maintainer: #randx
# App Version: 5.0
upstream gitlab {
server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen 90; # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
server_name git.mydomain.com; # e.g., server_name source.example.com;
server_tokens off; # don't show the version number, a security best practice
root /home/git/gitlab/public;
# individual nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
location / {
# serve static files from defined root folder;.
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html #gitlab;
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location #gitlab {
proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://gitlab;
}
}
NOTE: I am accessing the two domains from an OSX machine on the same local network which has entries in it's /etc/hosts file like so:
192.168.1.100 db.mydomain.com
192.168.1.100 git.mydomain.com
Try to use:
server_name git.mydomain.com:90;
... and:
server_name db.mydomain.com:90;

Resources