NGinx: set localhost/project to project git folder - nginx

I am currently working on a small website in my free time. For development, I keep all git repositories in ~/git. Is it possible to configurate nginx so that if I go to localhost/A or localhost/B, it would use ~/git/A and ~/git/B respectively?
I am using fedora and tried with a single repo, but I keep getting 404:
/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
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;
server {
listen 80;
server_name localhost;
root /home/MartenBE/git/;
index index.html index.php;
location /A/ {
alias /home/MartenBE/git/A/;
}
}
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 {
}
}
}

I think only following should work for you.
server {
listen 80;
server_name localhost;
root /home/MartenBE/git/;
index index.html index.php;
}
You don't need to add location module because you have already defined root. Now if you try localhost/A/index.html, Nginx will try to find it under /home/MartenBE/git/A/index.html.
What are the requests you are trying ?

First of all it is good to seperate site configuration from main nginx conf file.
Do the following..
Remove all server blocks from /etc/nginx/nginx.conf.
create a file in /etc/nginx/sites-available/yourconf with the following content.
server {
listen 80;
server_name localhost;
root /home/MartenBE/git/;
index index.html index.php;
}
Create a symlink to the config file inside /etc/nginx/sites-enabled/yourconf.
Remove /etc/nginx/sites-enabled/default & reload the nginx config.
NOTE: if you are using ubuntu run sudo service nginx reload

Related

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

nginx configuration does not start

I'm trying to setup NGINX server as benchmark to test client-server interaction. The root in the server contains a few thousand random html pages.
This is also my first work with applications like NGINX. I have been struggling to configure nginx for awhile now using this website [1] and the documentation of nginx.
To give you some more background, I setup nginx on my local machine and the installation on a specific-directory (called libs, bad naming -- I should change that.)
After starting nginx using ./sbin/nginx -c conf/nginx.conf I tried to curl on the website to check if it is functional
curl http://127.0.0.1:6011
And I get this error:
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.10</center>
</body>
</html>
Where am I going wrong in my configuration?
[1] https://www.slashroot.in/nginx-web-server-performance-tuning-how-to-do-it
worker_processes 32;
worker_rlimit_nofile 51200;
error_log /lustre1/nginx-benchmark/libs/logs/error.log;
error_log /lustre1/nginx-benchmark/libs/logs/error.log notice;
error_log /lustre1/nginx-benchmark/libs/logs/error.log info;
pid /lustre1/nginx-benchmark/libs/logs/nginx.pid;
events {
worker_connections 50000;
multi_accept on;
}
http {
include /lustre1/nginx-benchmark/libs/conf/mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
types_hash_max_size 2048;
#gzip on;
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 /lustre1/nginx-benchmark/libs/logs/access.log main;
server {
listen 6011 default_server;
listen [::]:6011 default_server ipv6only=on;
server_name localhost;
#listen 6011;
#server_name localhost;
#charset koi8-r;
access_log /lustre1/nginx-benchmark/libs/logs/host.access.log main;
location / {
root /lustre1/nginx-benchmark/dataset/1024/;
try_files $uri html/index.html;
#index.php;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /lustre1/nginx-benchmark/libs/html/50x.html {
root /lustre1/nginx-benchmark/libs/html;
}
}
}
Can you ls any files in /lustre1/nginx-benchmark/dataset/1024/?
ls -l /lustre1/nginx-benchmark/dataset/1024/
If you can't then, that's why nginx is 404ing your request - it can't see them either. If you can, what are the permissions on that folder and the files? Are they readable by the user nginx is running as? What about the parent folders of that path?
Add an error log with debug, to see what nginx thinks the problem is:
access_log /lustre1/nginx-benchmark/libs/logs/host.access.log main;
error_log /lustre1/nginx-benchmark/libs/logs/host.error.log debug;
Change your try_files line to look like this:
try_files $uri /index.html =404;
The =404 should terminate nginx's repeated checking, which is probably being caused by your /lustre1/nginx-benchmark/dataset/1024/ docroot not having a html/index.html in it.

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 _;
}

map subdomain to different applications running on same IP same port distingushed by path

I have 3 applications(one web application,2 angular apps) running on same ec2 instance on same port(8080)
Paths to apps are
http://53.233.23.12:8080/Abc
http://53.233.23.12:8080/Xyz
http://53.233.23.12:8080/Pqr
I am using Nginx for redirection in server
My nginx.conf file looks like this`
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/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;
include /etc/nginx/conf.d/*.conf;
index index.html index.htm;
server {
listen 80 default_server;
listen [::]:80 default_server;
include /etc/nginx/default.d/*.conf;
server_name www.listmydebt.com listmydebt.com;
return 301 http://listmydebt.com:8080/Abc;
# redirect server error pages to the static page /40x.html
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
server_name admin.listmydebt.com;
return 301 http://listmydebt.com:8080/Xyz;
}
server {
listen 80;
server_name partner.listmydebt.com;
return 301 http://listmydebt.com:8080/Pqr;
}
}
All domain and subdomains( listmydebt.com, admin.listmydebt.com , partner.listmydebt.com) pointing to same IP address(53.233.23.12).
My Nginx is running on port 80 and tomcat server in which my applications are deplyed running on port 8080
When I put listmydebt.com its redirecting to http://listmydebt.com:8080/Abc and browser url changed(http://listmydebt.com:8080/Abc). But what I want is the url on browser should remain same as listmydebt.com but it should show the redirected url content.same happening for subdomains as well.
Please help me out .If any additional info is required please mention.Thanks in advance.
The best way is to use docker swarm, or at least run the applications as several dockers in several ports.
Then you can easily map the ports on subdomains with an ansible playbook.

Resources