What is Nginx Virtual Host
Where is the Nginx Server Block
Server Block in Nginx is equivalent to VirtualHost in Apache HTTPD server. You can run multiple websites using a server block inside a single Nginx Installation.
How can we add multiple hosts to Nginx?
You can host multiple websites on a single machine using Nginx Server Block. You need to add a separate “Server Block” for each website in the Nginx configuration section.
How to Add a server Block
Please create a server block into this path sudo vi /etc/nginx/conf.d/sameple.com.conf
server {
listen 80;
server_name u.awswithatiq.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
To create a Virtual Server Block for PHP-FPM then add this code in the /etc/nginx/conf.d/sameple.com.conf file
server {
listen 80;
server_name www.gcptips.com;
rewrite ^ $scheme://gcptips.com$request_uri?;
}
server {
listen 80;
server_name gcptips.com;
root /var/www/wordpress;
index index.php;
charset UTF-8;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
# fastcgi_intercept_errors on;
# fastcgi_keep_conn on;
# fastcgi_read_timeout 300;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
#for ubuntu unix:/var/run/php/php8.0-fpm.sock;
##
# FastCGI cache config
##
# fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:10m max_size=1000m inactive=60m;
# fastcgi_cache_key $scheme$host$request_uri$request_method;
# fastcgi_cache_use_stale updating error timeout invalid_header http_500;
fastcgi_cache_valid any 30m;
}
}
To enable Gzip compression please use these lines under the server block
# Enable Gzip
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_proxied any;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
gzip_static on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
Add expires by adding the following lines in your code
. . .
# Default server configuration
#
# Expires map
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
listen 80 gcptips.com;
listen [::]:80 gcptips.com;
expires $expires;
For WordPress URL rewrite, please add the following lines inside the server block
location / {
try_files $uri $uri/ /index.php?$args;
}
To check the error log, please run this command
sudo tail -30 /var/log/nginx/error.log
A detailed explanation is given here.
Pingback: How to Setup PHP-FPM (PHP 8) Nginx in Amazon Linux 2 (updated June 2021 ) - AWS with Atiq