nginx on vagrant: request not reaching virtual host/server block - nginx

I'm running an instance of nginx on a vagrant setup currently. I'm trying to have my nginx run two different URLs dev.v1.example.com and dev.v2.example.com by setting up virtual hosting/server blocks for them
The vagrant port forwarding works fine and I can reach the nginx from my host machine
config.vm.network "forwarded_port", guest: 80, host: 8980
So of course that means for me to access them on a browser on the host machine I'd have to type in dev.v1.example.com:8980 for example
My nginx.conf on the guest is your standard default nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
I have the following file at conf.d/vhost.conf
server {
listen 80;
server_name dev.v1.example.com;
root /vagrant/dev_v1;
}
server {
listen 80;
server_name dev.v2.example.com;
root /vagrant/dev_v2;
}
Whenever I request dev.v1.example.com:8980 or dev.v2.example.com:8980 from the host I never hit the server block and always get the default nginx start page which is what the default server block points to. It is never able to display the content for each specific server name in their respective root folders, but I would think the server block gets hit with the requests given how it's configured.
I've tried changing the listen ports in the vhost.conf to 8980 as well and it doesn't work along with changing the server name to append the 8980 port which always doesn't change anything, and I can see the 'Host' header in the requests I'm making is dev.v1.example.com:8980 and that's also what comes up in the nginx logs in the vagrant guest
Any ideas?
Thanks in advance

Create a reverse proxy on your host with two upstream :
upstream dev1__upstream {
server 192.168.33.2:80;
keepalive 64;
}
upstream dev2__upstream {
server 192.168.33.3:80;
keepalive 64;
}
and simply create vhost with a proxy pass :
server {
listen 80;
server_name dev1.local;
access_log /var/log/nginx/dev1.access.log;
error_log /var/log/nginx/dev2.error.log;
location / {
proxy_pass http://dev1__upstream;
}
}

Related

I need to configure inbound traffic to translate http to https with nginx and only need trust store from certificates

I am trying to translate inbound traffic from http to https, but i'am new to nginx and certification world, so it's not very clear to me when i was told that i need only trust store for this connection. I have ssl certificate. And forgot to mention. My OS is RHEL and using older version of java keystore(pkcs#7).
Here is my nginx.conf file. Any help would be great:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
#listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
#proxy_pass https://ipaddress:port/;
#proxy_set_header Token $http_token;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
server_name ip_address;
return 301 https://ipaddress:port/;
proxy_connect_timeout 60s;
proxy_send_timeout 120s;
proxy_read_timeout 120s;
location / {
proxy_pass https://ipaddress/;
proxy_set_header Token $http_token;
proxy_ssl_trusted_certificate /etc/nginx/ssl/keystore/new.pem;
proxy_ssl_verify on;
proxy_ssl_verify_depth 2;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/nginx.conf;
#
# location / {
# proxy_pass https://40.127.129.131:9444/;
# proxy_ssl_trusted_certificate /etc/nginx/ssl/keystore/novi.pem;
# proxy_ssl_verify on;
# proxy_ssl_verify_depth 2;
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
P.S.
In .pem file is certificate and pkcs#7(public key)

NGINX reverse proxy setting not redirecting to specified target, still shows default centos index page

I have a simple reverse setting on my GCE:
server {
listen 80;
location / {
proxy_pass http://192.168.49.2:31968/;
}
}
I have tested and restarted the service:
sudo service nginx restart
But when I try and curl http://localhost, I still get the default Centos index page rather than expected content from http://192.168.49.2:31968/
I have checked my nginx.conf to see if there is any weird settings overriding but don't see anything strange:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /404.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
Any ideas what's going on?
When you say you get the 'Centos index page', you mean the default 'Welcome to Nginx' page?
So I guess you want to override the default page?
Don't change the nginx.conf.
Go to /etc/nginx/sites-available, and there you find a default file.
change the content of the file to:
server {
listen 80;
location / {
proxy_pass http://192.168.49.2:31968/;
}
}

Confirmed nginx server blocks are working but one domain is still pointing back at the default web root

Currently i have two domains pointed at this server
website1 and website2
So the second website will go to its server block files as should
However the first one is still going back to the default location and i cannot locate where in the config files it would still be pointing to it
nginx.conf
https://paste.centos.org/view/752c15c1
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
#root /usr/share/nginx/html;
#root /var/www/nerdarcadia/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
website1.conf
https://paste.centos.org/view/03dd5162
server {
listen 80;
server_name nerdarcadia.com www.nerdarcadia.com;
#charset
#access_log /var/log/nginx/host.access.log main;
location / {
root /var/www/nerdarcadia/html
index index.html index.htm;
try_files $uri $uri/ =404;
}
#error_page 404 /404.html;
#redirect server error pages
#
###########
###BLANK###
###########
The way i solved it was using the config for the site that was working. I'm not sure what changed but a lot of these tutorials have out of date formats that throw errors
For anyone looking to "just get the server block working" this is what i used
server {
listen 80;
listen [::]:80;
root /var/www/website2/html;
index index.html;
server_name website2.com www.website2.com;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
Also make sure your sym link isn't broken between sites available/enabled folders

How to modify /etc/nginx/conf.d/*.conf to make nginx work as a load balancer?

I have two servers running the same web service, and I have another server running nginx, which redirects user requests to one of the two web servers.
/------> web server1 (10.56.0.1)
user --> nginx
\------> web server2 (10.56.0.2)
I can make it work by adding upstream and proxy_pass directly to /etc/nginx/nginx.conf.
http {
...
upstream backend {
server 10.56.0.1:80;
server 10.56.0.2:80;
}
server {
...
location / {
proxy_pass http://backend;
}
}
}
But I don't think it's a good idea to directly change /etc/nginx/nginx.conf, I want put upstream and proxy_pass in /etc/nginx/conf.d, but it doesn't work, why? My complete configurations are as follows.
/etc/nginx/nginx.conf (unmodified)
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
/etc/nginx/conf.d/default.conf
upstream backend {
server 10.56.0.1:80;
server 10.56.0.2:80;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
You did not mention any server_name in the file "/etc/nginx/conf.d/default.conf", on the other hand you have
server_name _;
block /etc/nginx/nginx.conf, so all traffics are pointed in server block of nginx.conf file.
I would like to suggest to remove below line from nginx.conf and put it in default.conf
server_name _;
So your default.conf will be
upstream backend {
server 10.56.0.1:80;
server 10.56.0.2:80;
}
server {
listen 80;
server_name _;
location / {
proxy_pass http://backend;
}
}
For more look at https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/
Thanks to the hint given by #solvease, I solved the problem by commenting the server block in /etc/nginx/nginx.conf and adding the server_name in /etc/nginx/conf.d/default.conf.
Changes in /etc/nginx/nginx.conf:
# server {
# listen 80 default_server;
# listen [::]:80 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
Changes in /etc/nginx/conf.d/default.conf:
server {
...
server_name _;
}

mkdocs nginx reverse proxy slow

I have setup mkdocs and running on port 8000, Nginx is setup as reverse proxy with below configuration. However when accessing the site through reverse proxy browser stays "connecting..." for a long time approx 2 mins and page loads. Also if I stop with "X" with the browser, entire page shows up immediately. Could anyone help on this please?
server {
listen 80;
server_name docs.example.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 $http_host;
proxy_pass http://127.0.0.1:8000/;
proxy_redirect off;
proxy_buffering off;
}
}
And nginx.conf is
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
MkDocs is a static site generator.
The intended use case is that you "build" your pages as static web pages and then upload the already built HTML pages to your server where the server serves them up to your users. A major advantage is that as the server does not need to reprocess the Markdown and templates for each request, it is much faster.
While it is true that MkDocs does include a serve command, the included "server" is intended for development only. In other words, when writing your documents you can use the development server to see your changes live on your local machine. However, the server is not intended to serve anything to other machines or the outside world. It was never anticipated that the server would have multiple simultaneous connections.
So rather than having nginx listing on a port, you should point it at a directory of static files and copy a build of your MkDocs documents to that directory.

Resources