Apache2 Nginx配置多域名
时间:2015-09-28 22:50 来源:linux.it.net.cn 作者:IT
两个web服务器同时运行。一个Apache2是9000端口,另一个Nginx是80端口。php是由Apache解析的。所以其实是分别配置 Apache2和Nginx的多个域名即可。就是说Nginx的80端口域名(如domain.com)和Apache2的9000(如 domain.com:9000)能同时指向一个相同的页面就成功了。按照Apache和Nginx的分别配置即可,当然前提是Nginx能正确转发到 Apache的端口。
1.Nginx配置域名
Nginx的配置文件位置(根据不同的安装位置而定):/etc/nginx/nginx.conf
按理应该将内容写入nginx.conf,但是看了一下两行
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
就知道网址配置文件的内容其实是可以单独作为一个文件使用的。/etc/nginx/sites-enabled/是放置域名配置文件的,默认文件为default,Nginx会加载/etc/nginx/sites-enabled/目录的所有文件。
所以在/etc/nginx/sites-enabled/目录下新建一个example.conf
内容如下:
server {
listen 80;
root /var/www/example;
server_name www.example.com;
index index.htm index.html;
location / {
root /var/www/example;
try_files $uri/index.html $uri.html $uri.php /index.php$uri?$args $uri @app;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
try_files $uri @app;
}
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/example/$fastcgi_script_name ;
#$document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
如果配置多个域名,则新建不同的配置文件,后缀不限,修改不同的域名和目录即可。
重启Nginx
/etc/init.d/nginx restart
如果能通过域名如www.example.com能访问服务器/var/www/example里面的静态文件,说明Nginx配置域名成功。不过目前还不能正常访问php的动态页面,还需要配置Apache2。
2.Apache2配置文件
Apache2的配置文件位置(根据不同的安装位置而定):/etc/apache2/apache2.conf
同样,你会看到apache2.conf里有一行(如果没有就添加上去)
IncludeOptional sites-enabled/*.conf
就是说Apache2会加载/etc/apache2/sites-enabled/里以.conf为后缀的所有文件新建一个host.conf的文件添加以下内容
<VirtualHost *:9000>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example
ServerName www.example.com
ErrorLog /var/www/example/error_log
TransferLog /var/www/example/access_log
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:9000>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example2
ServerName www.example2.com
ErrorLog /var/www/example2/error_log
TransferLog /var/www/example2/access_log
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
根据域名数量和指向的目录不同而添加不同的VirtualHost标签。
重启Apache2
/etc/init.d/apache2 restart
如果能通过域名+端口如www.example.com:9000能访问服务器/var/www/example里面的php或其他静态文件, 说明Apache配置域名成功。这样输入www.example.com也能正常访问动态的php文件,Apache+Nginx的多域名配置成功。
(责任编辑:IT)
两个web服务器同时运行。一个Apache2是9000端口,另一个Nginx是80端口。php是由Apache解析的。所以其实是分别配置 Apache2和Nginx的多个域名即可。就是说Nginx的80端口域名(如domain.com)和Apache2的9000(如 domain.com:9000)能同时指向一个相同的页面就成功了。按照Apache和Nginx的分别配置即可,当然前提是Nginx能正确转发到 Apache的端口。 1.Nginx配置域名 Nginx的配置文件位置(根据不同的安装位置而定):/etc/nginx/nginx.conf 按理应该将内容写入nginx.conf,但是看了一下两行 include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; 就知道网址配置文件的内容其实是可以单独作为一个文件使用的。/etc/nginx/sites-enabled/是放置域名配置文件的,默认文件为default,Nginx会加载/etc/nginx/sites-enabled/目录的所有文件。 所以在/etc/nginx/sites-enabled/目录下新建一个example.conf 内容如下: server { listen 80; root /var/www/example; server_name www.example.com; index index.htm index.html; location / { root /var/www/example; try_files $uri/index.html $uri.html $uri.php /index.php$uri?$args $uri @app; } location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ { try_files $uri @app; } location @app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } location ~ ^/(app|app_dev)\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /var/www/example/$fastcgi_script_name ; #$document_root$fastcgi_script_name; fastcgi_param HTTPS off; } } 如果配置多个域名,则新建不同的配置文件,后缀不限,修改不同的域名和目录即可。 重启Nginx /etc/init.d/nginx restart 如果能通过域名如www.example.com能访问服务器/var/www/example里面的静态文件,说明Nginx配置域名成功。不过目前还不能正常访问php的动态页面,还需要配置Apache2。 2.Apache2配置文件 Apache2的配置文件位置(根据不同的安装位置而定):/etc/apache2/apache2.conf 同样,你会看到apache2.conf里有一行(如果没有就添加上去) IncludeOptional sites-enabled/*.conf 就是说Apache2会加载/etc/apache2/sites-enabled/里以.conf为后缀的所有文件新建一个host.conf的文件添加以下内容 <VirtualHost *:9000> ServerAdmin webmaster@localhost DocumentRoot /var/www/example ServerName www.example.com ErrorLog /var/www/example/error_log TransferLog /var/www/example/access_log ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> <VirtualHost *:9000> ServerAdmin webmaster@localhost DocumentRoot /var/www/example2 ServerName www.example2.com ErrorLog /var/www/example2/error_log TransferLog /var/www/example2/access_log ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> 根据域名数量和指向的目录不同而添加不同的VirtualHost标签。 重启Apache2 /etc/init.d/apache2 restart 如果能通过域名+端口如www.example.com:9000能访问服务器/var/www/example里面的php或其他静态文件, 说明Apache配置域名成功。这样输入www.example.com也能正常访问动态的php文件,Apache+Nginx的多域名配置成功。 (责任编辑:IT) |