Centos

Nginx。在同一個埠上配置兩個站點

  • July 20, 2015

我正在嘗試在我的伺服器上添加一個 wikimedia 站點,該站點上已經有另一個站點(不使用 PHP)。我想把它放在一個不同的文件夾中,比如www.hostname.com/wiki,但是最好有兩個不同的 conf 文件以避免混淆。目前我正在嘗試將這個 wikimedia 站點放在另一個埠 81 上,並且遇到了一些問題。

我的nginx.conf看起來像這樣:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;

events {
   worker_connections  1024;
}

http {
   include       /etc/nginx/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  /var/log/nginx/access.log  main;
   sendfile        on;
   keepalive_timeout  65;
   index   index.html index.htm index.php;
   include /etc/nginx/conf.d/*.conf;

}

在 conf.d 文件夾中,我有兩個文件:

site.conf看起來像這樣:

server {
   listen 80;

   server_name hostname.com;

   auth_basic "Restricted Access";
   auth_basic_user_file /etc/nginx/htpasswd.users;

   location /kibanaadmin/ {
       proxy_pass http://localhost:5601/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host $host;
       proxy_cache_bypass $http_upgrade;
   }

   root /usr/share/nginx/html/pruebas/site;
   index index.php index.html index.htm;

   try_files $uri $uri/ /index.php?$args;

   location ~ \.php$ {
       try_files $uri =404;
       fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }
}

wiki.conf文件:

server {

       listen 81;

       server_name hostname.com;

       root /usr/share/nginx/html/tests/mediawiki;

       client_max_body_size 5m;
       client_body_timeout 60;

       index index.html index.htm index.php;

       location / {
               try_files $uri $uri/ @rewrite;
               autoindex on;
               index index.html index.html index.php;
       }

       location @rewrite {
               rewrite ^/(.*)$ /index.php?title=$1&$args;
       }

       location ^~ /maintenance/ {
               return 403;
       }

       location ~ \.php$ {
               autoindex on;
               include fastcgi_params;
               fastcgi_pass unix:/tmp/phpfpm.sock;
       }

       location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
               try_files $uri /index.php;
               expires max;
               log_not_found off;
       }

       location = /_.gif {
               expires max;
               empty_gif;
       }

       location ^~ /cache/ {
               deny all;
       }

       location /dumps {
               root /usr/share/nginx/html/tests/mediawiki/local;
               autoindex on;
       }
}

我注意到兩個不同的問題:

第一個是當我嘗試獲取一個 php 文件時,我得到一個502 Bad Gateway並且在 error.log 上我得到這個消息:

2015/07/15 10:56:57

$$ crit $$16306#0: *6 connect() to unix:/var/run/php-fpm/php-fpm.sock 在連接到上游時失敗(2:沒有這樣的文件或目錄),客戶端:111.222.333.444,伺服器:主機名.com,請求:“GET /info.php HTTP/1.1”,上游:“fastcgi://unix:/var/run/php-fpm/php-fpm.sock:”,主機:“hostname.com”

我檢查了我的 php 配置,我認為是正確的,我不知道是 nginx 配置問題還是 PHP。

第二個問題是,當我訪問 hostname.com:81 時,我得到了403 Forbidden(我在 hostname.com:80 中的其他站點正常工作),並且此日誌消息:

2015/07/15 10:59:15

$$ error $$16306#0: *9 的目錄索引

“/usr/share/nginx/html/pruebas/”被禁止,客戶端:111.222.333.444,伺服器:hostname.com,請求:“GET / HTTP/1.1”,主機:“hostname.com:81”

我的主要問題是:我怎樣才能設法在埠 80 和我的網站上擁有 wikimedia 站點,但使用 nginx 的兩個 conf 文件來做到這一點(如果不可能,我怎麼能只用一個文件來做到這一點)和在 nginx 中正確配置 PHP 的東西?

注意:我認為這不是權限問題,因為我給 wikimedia 網站提供了與其他網站相同的內容。我正在使用 Centos 7.1。先感謝您。

感謝您的回答。我解決了這兩個問題。第一個問題是 502 Bad Gateway,我通過重新安裝 PHP 模組並在 nginx 文件上編寫正確的配置來解決(查看 wiki 位置塊中的位置 ~* .php$ 部分)。

我終於只使用了一個配置文件,編寫了一個新的位置塊,並以正確的方式編寫了 PHP 配置。

使用此文件也解決了第二個問題(403)。

server {
   listen 80;

   server_name hostname;

   location /admin/ {

       auth_basic "Restricted Access";
       auth_basic_user_file /etc/nginx/htpasswd.users;

       root /usr/share/nginx/html/pruebas/admin;
       index index.php index.html index.htm;

       proxy_pass http://localhost:5601/;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
   }


   # the following line is responsible for clean URLs
   try_files $uri $uri/ /index.php?$args;

   location /site {
       alias /usr/share/nginx/html/site;
       auth_basic "Restricted Access";
       auth_basic_user_file /etc/nginx/htpasswd.users;
   }

   location /wiki {

       auth_basic "Restricted Access";
       auth_basic_user_file /etc/nginx/wiki/htpasswd;

       alias /usr/share/nginx/html/mediawiki;

       location ~* \.php$ {
         fastcgi_pass    127.0.0.1:9000;
         include         fastcgi.conf;
         fastcgi_param   SCRIPT_FILENAME $request_filename;
       }

       location ~* \.(js|css|png|jpg|jpeg|gif|ico|xml)$ {
           expires 1y;
               access_log off;
               log_not_found off;
       }
   }

   location ~ \.php$ {
       try_files $uri =404;
       fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }

}

將配置放在單獨的文件中的一種選擇是使用include指令。像這樣的東西:

server {
   listen 80;

   server_name hostname.com;


   auth_basic "Restricted Access";
   auth_basic_user_file /etc/nginx/htpasswd.users;

   location /kibanaadmin/ {
       proxy_pass http://localhost:5601/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host $host;
       proxy_cache_bypass $http_upgrade;
   }

   root /usr/share/nginx/html/pruebas/site;
   index index.php index.html index.htm;

   try_files $uri $uri/ /index.php?$args;

   location ~ \.php$ {
       try_files $uri =404;
       fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
   }

   include '/path/to/wiki.conf';
}

wiki.conf看起來像這樣:

location /wiki {
   alias /usr/share/nginx/html/tests/mediawiki;
   try_files $uri $uri/ @rewrite;
   autoindex on;
   index index.html index.html index.php;
}

location @rewrite {
   rewrite ^/(.*)$ /index.php?title=$1&$args;
}

location ^~ /maintenance/ {
   return 403;
}

location ~ \.php$ {
   autoindex on;
   include fastcgi_params;
   fastcgi_pass unix:/tmp/phpfpm.sock;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
   try_files $uri /index.php;
   expires max;
   log_not_found off;
}

location = /_.gif {
   expires max;
   empty_gif;
   }

   location ^~ /cache/ {
           deny all;
   }

   location /dumps {
           root /usr/share/nginx/html/tests/mediawiki/local;
           autoindex on;
   }
}

引用自:https://serverfault.com/questions/705863