2013-01-02 54 views
0

我配置nginx的服務於我們的服務器,有兩種VIRTUA主機:主主機和子域的主機。主要的主機是一個鐵路應用程序,與乘客一起服務。它按預期工作。nginx的禁止和壞網關錯誤

子域主機是一個小小的PHP應用程序。對這個子域做一個瀏覽器請求,它返回一個403(禁止)的錯誤。當對特定文件執行瀏覽器請求時,它將返回502(錯誤網關)錯誤。

這裏是nginx.conf文件:

#user nobody; 
worker_processes 3; 

events { 
    worker_connections 19000; 
} 

worker_rlimit_nofile 20000; 


http { 
    include  mime.types; 
    default_type application/octet-stream; 

    passenger_root /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.18; 
    passenger_ruby /usr/local/bin/ruby; 

    sendfile  on; 

    gzip on; 
    gzip_http_version 1.1; 
    gzip_disable  "msie6"; 
    gzip_vary   on; 
    gzip_comp_level  9; 
    gzip_static   on; 

    passenger_max_pool_size 6; 
    passenger_min_instances 1; 
    passenger_pool_idle_time 10; 

    # Rails app 
    server { 
    listen  80; 
    server_name .domain.com; 
    passenger_enabled on; 

    root /home/ubuntu/rails_app/public; 

    location ~ ^/assets/ { 
     expires   max; 
     add_header  Cache-Control public; 
     #add_header  Last-Modified ""; 
     #add_header  ETag ""; 
     open_file_cache   max=1000 inactive=500s; 
     open_file_cache_valid 600s; 
     open_file_cache_errors on; 
     break; 
    } 

    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
     root html; 
    } 
    } 


    # PHP app 
    server { 
     listen  80; 
     server_name sub.domain.com; 

     root /home/ubuntu/rails_app/sendy; 
     index index.html index.htm index.php; 

     if (!-d $uri) { 
      set $rule_0 1$rule_0; 
     } 

     if (!-f $uri) { 
      set $rule_0 2$rule_0; 
     } 

     if ($rule_0 = "21") { 
      rewrite ^/([a-zA-Z0-9-]+)$ /$1.php last; 
     } 

     location/{ 
      try_files $uri $uri/ /index.php; 
     } 

     location ~ \.php$ { 
      #root   html; 
      fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
      #fastcgi_index index.php; 
      fastcgi_param SCRIPT_NAME  $fastcgi_script_name; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      include  fastcgi_params; 
     } 

     location /l { 
      rewrite ^/l/([a-zA-Z0-9/]+)$ /l.php?i=$1 last; 
     } 

     location /t { 
      rewrite ^/t/([a-zA-Z0-9/]+)$ /t.php?i=$1 last; 
     } 

     location /w { 
      rewrite ^/w/([a-zA-Z0-9/]+)$ /w.php?i=$1 last; 
     } 

     location /unsubscribe { 
      rewrite ^/unsubscribe/(.*)$ /unsubscribe.php?i=$1 last; 
     } 

     location /subscribe { 
      rewrite ^/subscribe/(.*)$ /subscribe.php?i=$1 break; 
     } 

     location ~ /\.ht { 
      deny all; 
     } 
    } 
} 

我認爲這是一個權限問題,但我將其更改爲744,755和777甚至和仍然得到同樣的錯誤。

任何想法?

+0

什麼的'ps輔助輸出| grep「php」'?你的php-fpm是否正在運行,是否在你指定的套接字上監聽? –

+0

這是輸出,米歇爾: '的ubuntu 1531 0.0 0.1 8104 928個PTS/0 S + 14:3 0:00的grep --color =自動PHP 根7256 0.0 0.5 60672 3212? SS 2012 0:09 PHP-FPM:主進程(/etc/php5/fpm/php-fpm.conf) ' – betacar

回答

1

您的nginx配置在第一個視圖上看起來不錯。我有點擔心php-fpm過程。你應該有一個主進程和至少一個孩子,通常不止一個孩子。

你配置PHP-FPM池?

這是我的過程是如何的樣子

# ps aux | grep "php" 
root  1081 0.0 0.3 387316 5404 ?  Ss 14:49 0:00 php-fpm: master process (/etc/php-fpm.conf) 
nobody 1082 0.0 0.6 390376 10316 ?  S 14:49 0:03 php-fpm: pool poolname.com 
nobody 1083 0.0 0.6 390388 10360 ?  S 14:49 0:03 php-fpm: pool poolname.com 
nobody 1084 0.0 0.6 390392 10324 ?  S 14:49 0:02 php-fpm: pool poolname.com 

/etc/php-fpm.conf至少應包含這樣的:

include=/etc/php-fpm.d/*.conf 

[global] 
pid = /var/run/php-fpm/php-fpm.pid 
error_log = /var/log/php-fpm.log 
daemonize = yes 

然後,你需要在/etc/php-fpm.d/

例如創建一個池定義mydomain.com.conf

[mydomain.com] 
; The address on which to accept FastCGI requests. 
; Valid syntaxes are: 
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on 
;       a specific port; 
; 'port'     - to listen on a TCP socket to all addresses on a 
;       specific port; 
; '/path/to/unix/socket' - to listen on a unix socket. 
; Note: This value is mandatory. 

listen  = /var/run/php-fpm/php-fpm.sock 

; Set permissions for unix socket, if one is used. In Linux, read/write 
; permissions must be set in order to allow connections from a web server. Many 
; BSD-derived systems allow connections regardless of permissions.. 
; Default Values: user and group are set as the running user 
; mode is set to 0666 

listen.owner = nginx 
listen.group = nginx 
listen.mode = 0666 

; Unix user/group of processes 
; Note: The user is mandatory. If the group is not set, the default user's group 
; will be used. 

user   = nobody 
group   = nobody 

; Choose how the process manager will control the number of child processes. 
; Possible Values: 
; static - a fixed number (pm.max_children) of child processes; 
; dynamic - the number of child processes are set dynamically based on the 
;    following directives: 
;    pm.max_children  - the maximum number of children that can 
;         be alive at the same time. 
;    pm.start_servers  - the number of children created on startup. 
;    pm.min_spare_servers - the minimum number of children in 'idle' 
;         state (waiting to process). If the number 
;         of 'idle' processes is less than this 
;         number then some children will be created. 
;    pm.max_spare_servers - the maximum number of children in 'idle' 
;         state (waiting to process). If the number 
;         of 'idle' processes is greater than this 
;         number then some children will be killed. 
; Note: This value is mandatory. 

pm = dynamic 

; The number of child processes to be created when pm is set to 'static' and the 
; maximum number of child processes to be created when pm is set to 'dynamic'. 
; This value sets the limit on the number of simultaneous requests that will be 
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork. 
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP 
; CGI. 
; Note: Used when pm is set to either 'static' or 'dynamic' 
; Note: This value is mandatory. 

pm.max_children = 100 

; The number of child processes created on startup. 
; Note: Used only when pm is set to 'dynamic' 
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers)/2 

pm.start_servers = 20 

; The desired minimum number of idle server processes. 
; Note: Used only when pm is set to 'dynamic' 
; Note: Mandatory when pm is set to 'dynamic' 

pm.min_spare_servers = 5 

; The desired maximum number of idle server processes. 
; Note: Used only when pm is set to 'dynamic' 
; Note: Mandatory when pm is set to 'dynamic' 

pm.max_spare_servers = 100 

; The number of requests each child process should execute before respawning. 
; This can be useful to work around memory leaks in 3rd party libraries. For 
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS. 
; Default Value: 0 

pm.max_requests = 250 

如果配置設爲您的完全滿意重啓PHP-FPM

/etc/init.d/php-fpm restart 

你應該看到幾個過程,現在

ps aux | grep "php" 

確保套接字文件存在且由正確的用戶所擁有的(應該是用戶使用nginx的運行)

# ls -l /var/run/php-fpm/php-fpm.sock 
srw-rw-rw- 1 nginx nginx 0 Jan 2 14:49 /var/run/php-fpm/php-fpm.sock 

立即重新啓動的nginx,以確保它讀取新的套接字文件

/etc/init.d/nginx restart 

希望這有助於。 如果沒有,別的東西是錯誤的,我們還需要繼續調試

+0

我仍然得到502錯誤:( – betacar

+1

哎,我想幫你,但「仍不工作「不是我們可以繼續的迴應,請告訴你做了什麼,以及我提出的檢查是否都是肯定的 –

+0

我已經完成了您指定的所有步驟,我更改了文件所有者並檢查了所有內容。而日誌仍然顯示'* 9 connect()unix:/var/run/php-fpm/php-fpm.sock失敗(111:連接被拒絕)' – betacar