Nginx 多站點配置
阿新 • • 發佈:2019-02-16
一、建立站點配置檔案
1、在 nginx conf 目錄中建立 conf.d 資料夾以及配置檔案,這裡假設站點為 www.a.com
$ pwd
/usr/local/nginx/conf
$ sudo mkdir conf.d
$ sudo vim conf.d/www.a.com.conf #新站點配置檔案
2、將如下內容寫入 www.a.com.conf 檔案中,該內容可從 nginx.cnf 檔案中擷取 server 節點修改。
修改以下兩點:
server_name www.a.com; #此處 www.a.com 為訪問域名
root /home/www/www.a.com; #檔案存放目錄,此處 www.a.com 為資料夾名稱
修改結果如下:
server {
listen 9999;
server_name www.a.com; #此處 www.a.com 為訪問域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/www/www.a.com; #檔案存放目錄,此處 www.a.com 為資料夾名稱
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
二、建立Nginx配置檔案
在 http 節點最底部增加 include conf.d/*.conf; nginx配置檔案是順序載入的!
修改完畢,內容如下:
#user nobody;
worker_processes 1 ;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include /usr/local/nginx/conf/conf.d/*.conf; #載入多站點配置檔案
}
三、建立測試環境
1、建立測試網頁資訊
$ pwd
/home
$ sudo mkdir -p www/www.a.com
$ sudo sh -c "echo Hello World>www/www.a.com/index.html"
$ cat www/www.a.com/index.html #檢視寫入內容
Hello World
2、修改本地hosts 檔案,新增 www.a.com 跳轉到 127.0.0.1
t$ sudo vim /etc/hosts
$ cat /etc/hosts|grep www.a.com # 檢視修改結果
127.0.0.1 www.a.com
四、重啟 Nginx ,關於Nginx 服務配置參見 Ubuntu 12.04 Nginx 安裝記錄
$ sudo service nginx restart