Nginx的。 在同一个端口上configuration两个站点

我正在尝试在我的服务器上添加一个维基媒体网站,该服务器上已经有另一个网站(不使用PHP)。 我想把它放在不同的文件夹,如www.hostname.com/wiki,但它有两个不同的conf文件,以避免混合的东西将是美好的。 目前我正试图把这个维基媒体网站放在不同的港口,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错误的网关,并在error.log我得到这个消息:

2015/07/15 10:56:57 [暴击] 16306#0:* 6连接()到unix:/var/run/php-fpm/php-fpm.sock失败(2:没有这样的文件或目录),而连接到上游,客户端:111.222.333.444,server:hostname.com,请求:“GET /info.php HTTP / 1.1”,上游:“fastcgi:// unix:/ var / run / php -fpm / php-fpm .sock:“,主机:”hostname.com“

我检查了我的PHPconfiguration,我认为是正确的,我不知道这是一个nginxconfiguration问题或PHP。

第二个问题是,当我去hostname.com:81我得到和403禁止 (我在hostname.com:80其他网站工作没有问题)和这个日志消息:

2015/07/15 10:59:15 [error] 16306#0:* 9目录索引

“/ usr / share / nginx / html / pruebas /”被禁止,客户端:111.222.333.444,server:hostname.com,请求:“GET / HTTP / 1.1”,主机:“hostname.com:81”

我的主要问题是:我如何设置维基媒体网站的端口80以及我的网站,但使用两个Nginx的conf文件来做到这一点(如果不可能,我怎么能只用一个文件)和在nginx中正确configurationPHP的东西?

注意:我认为这不是一个权限问题,因为我给了维基媒体网站的其他网站一样。 我正在使用Centos 7.1。 先谢谢你。

在不同文件中configuration一个选项是使用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; } } 

感谢您的回答。 我解决了这两个问题。 第一个问题是502错误的网关,我通过重新安装PHP模块并在nginx文件中写入适当的configuration(检查wiki位置块中的位置〜* .php $ part)来解决。

我终于只使用了一个configuration文件,编写了一个新的位置块,并以正确的方式编写了PHPconfiguration。

第二个问题(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; } }