The problem
I have a website developed with NextJS, it is running on a server with Nginx. The website files are inside the public_html folder and running on port 3004, and i have a proxy_pass that redirect the requests to the NextJS website on port 3004.
But when I search for my domain or my site on google, it shows up in the results: Index of / (and the files inside)
I would like to remove this (and all the listing of files inside) to just put Home - Domain, for example.
Research and issue photos
My next.conf file
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 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
server_name computadorsolidario.tec.br www.computadorsolidario.tec.br;
location / {
autoindex off;
proxy_pass http://localhost:3004;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}
}
}
How can i solve this?
You can turn off the feature of nginx where it creates directory listings:
autoindex off;
If you want to replace "index if /" with an actual home page, you would need to create an index.html file in document root directory.
You should also add as a last location in your conf
location ~ /\. {
deny all;
}
to block file access to dot files .env file or other sensitive data
Related
I am trying to configure NGINX to serve my nest app(which is running on docker).
My app is listening on port 3000
The server is amazon linux 2(ec2-user)
The conf file looks like this:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
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 4096;
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/nginx.conf;
server {
listen 80;
listen [::]:80;
server_name <ip.adress>;
#web
location / {
add_header X-yahav $uri; # this gets mounted
}
#api
location = /api { # this one is never approached
add_header X-yahav "Api-pass";
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header Host $http_host;
#proxy_pass http://127.0.0.1:3000/;
}
}
}
I want to redirect /api to my nest app but it just wont have it i'm getting a simple 404 without the header i'm attaching(as you can see in the conf file)
Another thing is when I go to the root (location /) I do get my header mounted as expected
Any have any idea what is wrong?
Don't forget to add "/api" to your requests, like http://your-server:80/api/
I have an html web server running on my EC2 instance on port 80 (HTTP). I'm using Nginx as a reverse proxy (which is also installed on the instance) to redirect requests from port 8080 based on the path ('/blue') to my HTML web server. So I'm requesting HTTP://ec2_instance_dns:8080/blue and expect to redirect to HTTP://ec2_instance_dns:80.
However, I'm having issues redirecting the request based on the path variable. My /etc/nginx/conf.d/deafult.conf attached below:
server {
listen 8080;
server_name default;
client_max_body_size 32M;
underscores_in_headers on;
root /var/www/html/index.html;
location /blue {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_read_timeout 120;
proxy_next_upstream error timeout invalid_header http_500;
proxy_connect_timeout 90;
proxy_pass http://localhost:80;
}
}
My /etc/nginx/nginx.conf attached bellow:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
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;
server {
listen 8080;
listen [::]:8080;
server_name localhost;
root /var/www/html/index.html;
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 {
}
}
}
When trying to request HTTP://ec2_instance_dns:8080/blue I'm getting the above error:
What further configuration should I do? Thank you.
I am trying to make nginx have two functions like fiddler does:
1、Redirect requests from data.abc.com to 127.0.0.1:9000
2、Pass all other requests to their original servers
my nginx.conf is:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
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;
server {
listen 8008;
server_name data.abc.com;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass https://127.0.0.1:9000/;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
However, right now every request via port 8008 is redirected, it seems like server_name doesn't work, how to make other requests go to original server?
Your configuration is to redirect all request to https://127.0.0.1:9000
Add 2 different server block like follows
1) Redirect data.abc.com to https://127.0.0.1:9000
server {
listen 8008;
server_name data.abc.com;
return 301 https://127.0.0.1:9000$request_uri
}
2) Serve request for another website:
server {
listen 8008 default_server;;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
}
I'm trying to use Nginx as a reverse proxy in and centos 6.9_64 environment where clients connects to my server (http://www.51ti.vip).
Nginx will forward all requests to other backend server. The communication is working on port 80.
However, once proxy_set_header XXXXX is set, it will appear 403 when accessed.
There is no relevant error information in /var/log/nginx/error.log.
Where's the problem?
Page 403 Forbidden
You don't have permission to access the URL on this server.
Note:
OS System: CentOS 6.9_64
Nginx version 1.10.2
Config:
/etc/nginx/nginx.conf as follows:
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
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;
}
/etc/nginx/conf.d/default.conf as follows:
server {
listen 80 default_server;
server_name 47.75.249.199 "";
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://sq.otherserver.com;
#Proxy Settings
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;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
The original problem was caused by "proxy_set_header Host $host",
there's no problem whith proxy_set_header X-Real-IP and proxy_set_header X-Forwarded-For.
But still don't understand why?
Nginx: when to use proxy_set_header Host $host vs $proxy_host
Module ngx_http_proxy_module
What I am trying to do using nginx is- to call a backend for authentication and if the response is successful I will redirect to website 1 (for example -google.com) and if authentication fail I will redirect to website2(facebook for example).
Below is my nginx.conf-
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
include /etc/nginx/conf.d/default.conf;
}
The default.conf file is as below -
server {
listen 80 default_server;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://backend_ip_Address;
set $my_var 0;
if ($status = "200"){
set $my_var 1;
}
#if($status = 4xx) {
# set $my_var 2;
#}
if ($my_var = 1){
proxy_pass http://www.google.com;
}
if ($my_var = 2) {
proxy_pass http://www.facebook.com;
}
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
The issue I am facing is when I am trying to execute sudo service nginx restart with this configuration I am getting below error-
Starting nginx: nginx: [emerg] unknown "status" variable
The same $status is also present in nginx.conf log configuration and it's logging the response code properly like 301, 200 etc. But the same status variable is not working in default.conf file. Any help on what I am doing wrong?
I tried replacing status with body_bytes_sent header and it's works.
By google search https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=nginx++unknown+%22status%22+variable only related information is https://www.drupal.org/node/2738983 but no much help to resolve this.
status variable is defined on very late phase, after request is processed and response is ready to sent back.
You cannot use it for conditional routing.
Usually it's used for logging.
Here you may read about nginx directives execution order and phases:
https://openresty.org/download/agentzh-nginx-tutorials-en.html