Answer the question
In order to leave comments, you need to log in
How to apply the same settings to all hosts at once in Nginx?
I have a server with several sites in sites-available
I want to deny access to some locations and issue 404 or 444
But I don’t want to write the same thing in each file
I read the Internet, it seems that the issue can be solved through nginx.conf
I write there:
server {
location /123 {
deny all;
return 444;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
client_max_body_size 100M;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
server {
root /var/www/адрес;
index index.php index.html index.htm index.nginx-debian.html;
server_name адрес www.адрес;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/адрес/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/адрес/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}server {
if ($host = www.адрес) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = адрес) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name адрес www.адрес;
listen 80;
return 404; # managed by Certbot
}
Answer the question
In order to leave comments, you need to log in
once all the configs will have to be supplemented with the instruction include common.conf;
Create this file common.conf
and put general instructions in it.
Alternatively, write a script that will insert an addition into each config.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question