This is how I create am Nginx virtual host for a WordPress webapp. I did best to keep it as minimal as I can while having a working webapp. Please review it:
#!/bin/bash
domain="1ドル" && test -z "$domain" && exit 2
drt="/var/www/html"
cat <<-EOF > "$s_a/$domain.conf"
server {
root ${drt}/${domain};
server_name ${domain} www.${domain};
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
expires 365d;
}
}
EOF
1 Answer 1
You are putting your server_name
to be $domain
and www.$domain
. What if, someone puts in www.example.com
for the script argument? You will thne be serving www.example.com
and www.www.example.com
. Instead; replace possible www.
from the domain
variable.
domain="${domain#www\.}"
Each of your location
section has a different indentation. Keep it consistent. Since server
section is the root level, you do not need to indent it out. I understand that you are flushing the left indentation from the conf
, but I would suggest keeping it, since one might want to peek at them in future, and would like to tweak it?
#!/bin/bash
domain="1ドル" && test -z "$domain" && exit 2
domain="${domain#www\.}"
drt="/var/www/html"
cat <<EOF > "$s_a/$domain.conf"
server {
root "${drt}/${domain}";
server_name ${domain} www.${domain};
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~* \.(jpe?g|png|gif|ico|css|js|ttf|woff|pdf)$ {
expires 365d;
}
}
EOF
-
\$\begingroup\$ Hi, thanks ! Please elaborate on "Each of your location section has a different indentation. Keep it consistent". \$\endgroup\$Arcticooling– Arcticooling2018年03月09日 05:33:37 +00:00Commented Mar 9, 2018 at 5:33
-
\$\begingroup\$ @user9303970 What I meant was, two of your
location
sections have a 4-space further indented closing}
, whereas the last one doesn't. \$\endgroup\$hjpotter92– hjpotter922018年03月09日 10:36:40 +00:00Commented Mar 9, 2018 at 10:36 -
\$\begingroup\$ Oh yes, this is a mistake and I found myself a few days ago and fixed. Good you noted that anyway. \$\endgroup\$Arcticooling– Arcticooling2018年03月09日 10:37:25 +00:00Commented Mar 9, 2018 at 10:37
-
\$\begingroup\$ It was important for me inident the
server
in this case because the heredocument will de-indent it in execution. It will de-indent everything in exeuction to be honest, due to the hyphen in<<-
. \$\endgroup\$Arcticooling– Arcticooling2018年03月09日 10:39:17 +00:00Commented Mar 9, 2018 at 10:39 -
\$\begingroup\$ which is why I mentioned: "I understand that you are flushing the left indentation from the conf, but I would suggest keeping it, since one might want to peek at them in future, and would like to tweak it?" \$\endgroup\$hjpotter92– hjpotter922018年03月09日 10:41:05 +00:00Commented Mar 9, 2018 at 10:41
Explore related questions
See similar questions with these tags.