运行 nginx 时遇到问题

运行 nginx 时遇到问题

我在 EC2 上运行 nginx,目的是设置 phabricator。这是我的 nginx.conf 文件:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user  eng;
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        /var/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;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

# another virtual host using mix of IP-, name-, and port-based configuration
include /etc/nginx/sites-enabled/*;

}

这是conf.d:

 " ============================================================================
 " Netrw Directory Listing                                        (netrw v134)
 "   /etc/nginx/conf.d
 "   Sorted by      name
 "   Sort sequence: [\/]$,\.h$,\.c$,\.cpp$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
 "   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:exec   
 " ============================================================================
 ../
 virtual.conf

在我的 sites-enabled 中,我有一个指向配置 phabricator.mysite.com 的符号链接,该配置位于 sites-available 中。以下是 phabricator.mysite.com 的内容:

 ## Phabricator
server {
    listen       80;
    server_name  phabricator.mysite.com;

    root   /home/eng/phabricator/phabricator/webroot;
    try_files $uri $uri/ /index.php;

    location / {
        index  index.php;
        if ( !-f $request_filename )
        {
            rewrite ^/(.*)$ /index.php?__path__=/$1 last;
            break;
        }
    }

    location /index.php {
        fastcgi_pass   localhost:9000;
        fastcgi_index   index.php;

        #required if PHP was built with --enable-force-cgi-redirect
        fastcgi_param  REDIRECT_STATUS    200;

        #variables to make the $_SERVER populate in PHP
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  QUERY_STRING       $query_string;
        fastcgi_param  REQUEST_METHOD     $request_method;
        fastcgi_param  CONTENT_TYPE       $content_type;
        fastcgi_param  CONTENT_LENGTH     $content_length;

        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

        fastcgi_param  REMOTE_ADDR        $remote_addr;
    }
}

当我尝试访问 phabricator.mysite.com 时,我的 nginx 错误日志中出现以下错误:

2013/08/29 04:12:52 [error] 30977#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 50.240.220.206, server: phabricator.mysite.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "phabricator.mysite.com"

我的 webroot 文件夹的权限设置为 755。

我究竟做错了什么?

答案1

这是我在以下回答的复制/粘贴https://stackoverflow.com/questions/22090350/nginx-unable-to-open-primary-script/31907528#31907528。我在让 Phabricator 设置 Google Cloud 盒时实际上也遇到了同样的问题。

SELinux 默认会在 CentOS/RHEL 7+ 上导致此错误 :(

要测试 SELinux 是否是导致问题的原因,请执行以下操作

setenforce 0

... 看看一切是否正常。如果这样解决了问题,你可以关闭 SELinux(很弱,你比这更好),或者你可以用以下命令重新打开它

setenforce 1

...然后正确修复该问题。

如果你

tail -f /var/log/audit/audit.log

...您将看到 SELinux 问题。就我而言,它拒绝 PHP-FPM 访问 Web 文件。您可以运行以下指令来修复它:

setsebool -P httpd_can_network_connect_db 1
setsebool -P httpd_can_network_connect 1

一开始这并没有帮我解决这个问题,但后来恢复了 SELinux 上下文就解决了

restorecon -R -v /var/www

希望有所帮助。

答案2

在位置部分的“location /index.php {”后添加以下行

根/home/eng/phabricator/phabricator/webroot;

相关内容