1. 程式人生 > >Linux伺服器下Nginx與Apache共存

Linux伺服器下Nginx與Apache共存

同一個埠是不能同時有兩個程式監聽的。所以換個思路解決同一臺伺服器下某些網站執行在nginx下,某些網站執行在Apache下共存。

解決思路:

將nginx作為代理伺服器和web伺服器使用,nginx監聽80埠,Apache監聽除80以外的埠,我這暫時使用8080埠。

這裡寫圖片描述

解決方案:

  • 在Linux 一經搭建好環境 先後安裝了Nginx 和Apache 由於 預設埠都是:80
  • 一般客戶請求的伺服器埠預設為80 所以Nginx作為靜態頁埠設定:80;Apache設定埠為:8080(在httpd.conf 檔案中修改Listen:8080)
  • Apache下的網站:

在nginx.conf中 新增

  server {
            listen       80;
            server_name  www.one.ityangs.cn one.ityangs.cn;

location / {            
            proxy_pass              http://127.0.0.1:8080;              
            proxy_redirect          off;        
            proxy_set_header Host $host;       
            proxy_set_header X-Real
-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

在httpd.conf中 新增

<virtualhost *:8080>
ServerName  www.one.ityangs.cn
ServerAlias  www.one.ityangs.cn one.ityangs.cn 
DocumentRoot  /www/one
DirectoryIndex index.php index.html
<Directory /www/one>
Options +Includes +FollowSymLinks -Indexes AllowOverride All Order Deny,Allow Allow from All </Directory> </virtualhost>
  • Nginx下的網站:

在nginx.conf中 新增

 server {
        listen       80;
        server_name    two.ityangs.cn www.two.ityangs.cn;
        root   /www/two;
        location /{

            index  index.html index.htm index.php;
             if (!-e $request_filename) {
             rewrite  ^(.*)$  /index.php?s=$1  last;
             break;
            }
            error_page 404  /var/www/html/404.html;
        }


        location ~ \.php(.*)$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO  $fastcgi_path_info;
                fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                include        fastcgi_params;  
        }


}