760176104 发表于 2016-12-25 12:45:43

Nginx 同一主机部署多个应用

  近日有一需求,需要在一台主机上用nginx部署2个php应用,分别是wordpress和wiki,探索了半天,终于部署好了,下面把过程记录下来。
  1.   在nginx下创建vhosts目录,用以放置vhost文件。

mkdir vhosts
  2.   修改nginx.conf的配置, 在http节点增加下面内容设置,用来包含vhosts里的配置文件

# vhosts files
include /usr/local/nginx/vhosts/*;
   
  3.  在vhosts下创建testwp.com.conf和duwiki.com.conf文件, 注意:这里的配置文件以conf结尾
  testwp.com.conf

server{
listen       80;
server_namewww.testwp.com;
location / {
root   /Users/jiangzhiqiang/phpwork/wordpress;
indexindex.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root         /Users/jiangzhiqiang/phpwork/wordpress;
fastcgi_pass   127.0.0.1:9000;
fastcgi_indexindex.php;
# $document_root指前面的root路径
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
include      fastcgi_params;
}
}
   
  duwiki.com.conf

server {
listen80;
server_namewww.duwiki.com;
location / {
root   /Users/jiangzhiqiang/phpwork/dokuwiki;
indexindex.php index.html index.htm;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root         /Users/jiangzhiqiang/phpwork/dokuwiki;
fastcgi_pass   127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
include      fastcgi_params;
}
# deny access to files, dokuwiki settings
location ~ /(data|conf|bin|inc)/
{
deny all;
}
}

   
  carbon.conf

server {
listen80;
server_namewww.carbon.com;
location / {
root   /Users/jiangzhiqiang/phpwork/carbon;
indexindex.php index.html index.htm;
}
# nginx configuration
error_page 404 /404.php;
rewrite ^([^\.]*)/dashboard$ $1/dashboard.php;
rewrite ^([^\.]*)/favorites(/page/(*))?$ $1/favorites.php?page=$3 last;
rewrite ^([^\.]*)/json/(+)$ $1/json.php?action=$2 last;
rewrite ^([^\.]*)/login$ $1/login.php;
rewrite ^([^\.]*)/manage$ $1/manage.php;
rewrite ^([^\.]*)/new$ $1/new.php;
rewrite ^([^\.]*)/notifications$ $1/notifications.php;
rewrite ^([^\.]*)/page/(+)$ $1/index.php?page=$2 last;
rewrite ^([^\.]*)/register$ $1/register.php;
rewrite ^([^\.]*)/reply$ $1/reply.php;
rewrite ^([^\.]*)/robots.txt$ $1/robots.php;
rewrite ^([^\.]*)/search.xml$ $1/open_search.php;
rewrite ^([^\.]*)/search/(.*?)(/page/(*))?$ $1/search.php?keyword=$2&page=$4 last;
rewrite ^([^\.]*)/settings$ $1/settings.php;
rewrite ^([^\.]*)/sitemap-(topics|pages|tags|users|index)(-(+))?.xml$ $1/sitemap.php?action=$2&page=$4 last;
rewrite ^([^\.]*)/statistics$ $1/statistics.php;
rewrite ^([^\.]*)/t/(+)(-(*))?$ $1/topic.php?id=$2&page=$4 last;
rewrite ^([^\.]*)/tag/(.*?)(/page/(*))?$ $1/tag.php?name=$2&page=$4 last;
rewrite ^([^\.]*)/tags/following(/page/(*))?$ $1/favorite_tags.php?page=$3 last;
rewrite ^([^\.]*)/u/(.*?)$ $1/user.php?username=$2 last;
rewrite ^([^\.]*)/users/following(/page/(*))?$ $1/favorite_users.php?page=$3 last;
rewrite ^([^\.]*)/upload_controller$ $1/upload_controller.php;
#QSA is automatic in nginx.
rewrite ^([^\.]*)/view-(desktop|mobile)$ $1/view.php?view=$2 last;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root         /Users/jiangzhiqiang/phpwork/carbon;
fastcgi_pass   127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
include      fastcgi_params;
}
# deny access to files,settings
location ~ /(data|conf|bin|inc)/
{
deny all;
}
}

   
  4.  修改hosts文件

sudo vi /etc/hosts
127.0.0.1    www.testwp.com
127.0.0.1    www.duwiki.com
127.0.0.1    www.carbon.com
   
  5.   重启 nginx 

sudo nginx -s reload
   
  在浏览器url栏输入 www.testwp.com, 就访问到本地部署的wordpress应用,访问 www.duwiki.com,就访问到本地部署的 dokuwiki 应用。
页: [1]
查看完整版本: Nginx 同一主机部署多个应用