GNU/Linux >> Linux の 問題 >  >> Panels >> Panels

Nginx仮想ホスト–Nginxサーバーブロック

Nginxサーバーブロックとも呼ばれるNginx仮想ホストを作成する方法を説明します。以前のチュートリアルの1つで、Debian 6(squeeze)VPSにLNMP(Nginx、MySQL、PHP)サーバーをインストールして構成する方法を説明しました。次に、新しいサーバーブロック(仮想ホスト)を設定する方法を見ていきます。 )新しいドメインごとに。

注:「VirtualHost」はApacheの用語です。 Nginxには仮想ホストがなく、server_nameおよびlistenディレクティブを使用してtcpソケットにバインドする「サーバーブロック」があります。

次のスクリプトを使用して、Nginxサーバーに新しいサーバーブロックを設定できます。

#!/usr/bin/env bash
#
# Nginx - new server block
# http://rosehosting.com

# Functions
ok() { echo -e '\e[32m'$1'\e[m'; } # Green
die() { echo -e '\e[1;31m'$1'\e[m'; exit 1; }

# Variables
NGINX_AVAILABLE_VHOSTS='/etc/nginx/sites-available'
NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled'
WEB_DIR='/var/www'
WEB_USER='www-data'

# Sanity check
[ $(id -g) != "0" ] && die "Script must be run as root."
[ $# != "1" ] && die "Usage: $(basename $0) domainName"

# Create nginx config file
cat > $NGINX_AVAILABLE_VHOSTS/$1 <<EOF
server {
  server_name $1;
  listen 80;
  root $WEB_DIR/$1/public_html;
  access_log $WEB_DIR/$1/logs/access.log;
  error_log $WEB_DIR/$1/logs/error.log;
  index index.html index.php;
  location / {
    try_files \$uri \$uri/ @rewrites;
  }
  location @rewrites {
    rewrite ^ /index.php last;
  }
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }
  location ~ /\.ht {
    deny  all;
  }
  location ~ \.php {
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
  }
}
EOF

# Creating {public,log} directories
mkdir -p $WEB_DIR/$1/{public_html,logs}

# Creating index.html file
cat > $WEB_DIR/$1/public_html/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
        <title>$1</title>
        <meta charset="utf-8" />
</head>
<body class="container">
        <header><h1>$1<h1></header>
        <div id="wrapper"><p>Hello World</p></div>
        <footer>© $(date +%Y)</footer>
</body>
</html>
EOF

# Changing permissions
chown -R $WEB_USER:$WEB_USER $WEB_DIR/$1

# Enable site by creating symbolic link
ln -s $NGINX_AVAILABLE_VHOSTS/$1 $NGINX_ENABLED_VHOSTS/$1

# Restart
echo "Do you wish to restart nginx?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) /etc/init.d/nginx restart ; break;;
        No ) exit;;
    esac
done

ok "Site Created for $1"

簡単に言うと、次のようになります。

  1. サイトの新しいディレクトリを作成します(/var/www/DOMAIN.COM/public_html)
  2. ログファイル用の新しいディレクトリを作成します(/var/www/DOMAIN.COM/logs)
  3. 正しい所有者/グループを設定します。
  4. サイトが機能していることを示す簡単なindex.htmlファイルを作成します。
  5. 再起動を要求します。

スクリプトタイプを使用するには:

./nginx_vhost.sh  newdomain.com

このスクリプトは、Debian、Ubuntu、および密接に関連するディストリビューションで機能するはずです。

Ubuntu VPSホスティングプランのいずれかを使用する場合は、Nginx仮想ホストを自分で作成する必要はありません。専門のLinux管理者にNginxサーバーブロックの作成を依頼するだけです。 24時間年中無休でご利用いただけます。リクエストはすぐに処理されます。

PS。 この投稿が気に入った場合は、Nginx仮想ホストを作成する方法について、左側のボタンを使用してソーシャルネットワーク上の友達と共有するか、下に返信を残してください。ありがとう。


Panels
  1. nginx-413リクエストエンティティが大きすぎます

  2. Nginx仮想ホスト(サーバーブロック)を作成する方法

  3. WindowsにNginxをインストールする

  1. Nginxをインストールし、Ubuntu20.04で仮想ホストを構成します

  2. Nginxは特定のIPをホワイトリストに登録します

  3. Apacheで名前ベースの仮想ホストを設定する

  1. Apache仮想ホスト構成– Linux

  2. Linux Mint 13 / LinuxMint14にApacheWebサーバーをインストールします

  3. Ubuntu 20.04、Mysql 8、Ubuntu 20.04、NginxでWordPressをホストする