Nginx. 在同一端口上配置两个站点

Nginx. 在同一端口上配置两个站点

我正在尝试在我的服务器上添加一个维基媒体网站,该服务器上已经有另一个网站(不使用 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 文件夹中我有两个文件:

站点配置文件看起来像这样:

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配置文件文件:

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 [crit] 16306#0: *6 connect() to unix:/var/run/php-fpm/php-fpm.sock 失败 (2: 没有此文件或目录) 连接到上游时,客户端:111.222.333.444,服务器:hostname.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 禁止(我的另一个网站在 hostname.com:80 上运行没有问题)并且此日志消息:

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

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

我的主要问题是:如何才能让 wikimedia 网站和我的站点同时在端口 80 上运行,但要使用 nginx 的两个 conf 文件(如果不可能,如何只用一个文件来实现),并正确配置 nginx 中的 PHP 内容?

注意:我认为这不是权限问题,因为我给维基媒体网站的权限与其他网站相同。我使用的是 Centos 7.1。提前谢谢您。

答案1

将配置放在单独文件中的一种选择是使用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;
    }
}

答案2

感谢您的回答。我解决了这两个问题。第一个问题是 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;
    }

}

相关内容