2012-06-05 93 views
2

我的網站出現性能問題。在Ubuntu 11.04上,我的配置是1G VPS和wordpress/nginx/php-fpm。瓶頸是瀏覽器正在等待服務器的第一個字節。啓動連接後需要4-6秒才能等待服務器的第一響應(該網站是新的,並且接收流量非常低,大約每天50-150次訪問)。以下是我的nginx conf,我希望這可能有助於理解問題出在哪裏。我想知道這個配置有什麼問題可以優化。此外,如果任何人都可以推薦我使用分析/分析工具來適合我的配置。Nginx性能問題

注:我代替我的用戶名與myusername,我的域名與mydomain.com

nginx.conf

user myusername; 
worker_processes 4; 
pid /var/run/nginx.pid; 

events { 
    worker_connections 768; 
    # multi_accept on; 
} 

http { 


     index index.php index.html index.htm; 

    sendfile on; 
    # tcp_nopush on; 
    tcp_nodelay on; 
    keepalive_timeout 5; 
    types_hash_max_size 2048; 
    # server_tokens off; 

    # server_names_hash_bucket_size 64; 
    # server_name_in_redirect off; 

    include /etc/nginx/mime.types; 
    default_type application/octet-stream; 

    client_max_body_size 50m; 


    access_log /var/log/nginx/access.log; 
    error_log /var/log/nginx/error.log; 

    gzip on; 
    gzip_disable "msie6"; 

    # gzip_vary on; 
    # gzip_proxied any; 
    # gzip_comp_level 6; 
    # gzip_buffers 16 8k; 
    # gzip_http_version 1.1; 
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 

    ## 
    # Virtual Host Configs 
    ## 

    include /etc/nginx/conf.d/*.conf; 
    include /etc/nginx/sites-enabled/*; 
} 

sites-enabled/default

server { 

    listen 80; ## listen for ipv4; this line is default and implied 
    listen [::]:80 default ipv6only=on; ## listen for ipv6 
root /home/myusername/www; 

# Make site accessible from http://localhost/ 
server_name mydomain.com; 

location/{ 
    # First attempt to serve request as file, then 
    # as directory, then fall back to index.html 
    try_files $uri $uri/ /index.php; 
} 

location /doc { 
    # root /usr/share; 
    autoindex on; 
    allow 127.0.0.1; 
    deny all; 
} 

location /images { 
    # root /usr/share; 
    autoindex off; 
} 

error_page 404 = @wordpress; 
log_not_found off; 

location @wordpress { 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_param SCRIPT_FILENAME $document_root/index.php; 
     include /etc/nginx/fastcgi_params; 
     fastcgi_param SCRIPT_NAME /index.php; 
} 

location ^~ /files/ { 
     rewrite /files/(.+) /wp-includes/ms-files.php?file=$1 last; 
    } 


# redirect server error pages to the static page /50x.html 
# 
#error_page 500 502 503 504 /50x.html; 
#location = /50x.html { 
# root /usr/share/nginx/www; 
#} 

# proxy the PHP scripts to Apache listening on 127.0.0.1:80 
# 
#location ~ \.php$ { 
# proxy_pass http://127.0.0.1; 
#} 

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
# 
location ~ \.php$ { 
     try_files $uri @wordpress; 
     fastcgi_index index.php; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include /etc/nginx/fastcgi_params; 
} 

# deny access to .htaccess files, if Apache's document root 
# concurs with nginx's one 
# 
location ~ /\.ht { 
    deny all; 
} 
location ^~ /blogs.dir/ { 
     internal; 
     root /home/myusername/www/wp-content; 
    } 
} 

回答

2

看起來像一個WordPress網站,我更傾向於它是一個性能問題,而不是nginx配置本身。

一些建議:

1 - 請確保您有APC安裝並啓用

2 - 安裝服務器端緩存插件(W3的總緩存或Supercache),並將其配置爲使用APC作爲後盾存儲(並打開所有的緩存層)

就性能分析師而言,我是NewRelic的粉絲,他們的Pro級別在前2周內是免費的(通常足夠長以找到熱點)基本的性能信息永遠免費。

0

看來你的nginx配置確實有很大的改進空間。 Nginx已經非常有效地利用CPU和內存。但是,我們可以根據我們計劃提供的工作負載類型調整幾個參數。如果我們主要提供靜態文件,那麼我們期望我們的工作負載配置文件佔用更少的CPU資源,並且更多地面向磁盤進程實際上,只要nginx的本質是爲了達到最高性能,你的nginx.conf就不應該成爲問題,但正如你所說的,你根本沒有獲得nginx良好的性能。我也運行1GB-1核心VPS運行新鮮的LEMP安裝(Ubuntu 14.04,nginx,MySQL,php5-fpm,沒有其他人會考慮內存消耗,如cPanel,Zpanel和類似的,沒有phpMyAdmin以及(我使用MySQL Workbench應用程序)因此,我已經建立並運行了一個WordPress站點,沒有任何緩存插件甚至APC/memcached方案(仍然在研究適合我需求的最佳方法),而且我總是有一個優秀的性能

無論如何,爲了提高nginx性能,下面設置的nginx.conf仍然是一個非常基本的調整,這是我用來爲我的網站服務的當前nginx.conf文件的副本。它與你在這裏只是作爲參考,你可以根據你自己的研究進一步調整它,但我相信嘗試一下後,您肯定會注意到整體增強功能。

所以讓我們通過它...

TUNING nginx的

確定的Nginx worker_processes和worker_connections

我們可以配置單線程工作進程的數量爲1.5到2倍的CPU內核的數量,以利用磁盤帶寬的(IOPS)。

確保您在/etc/nginx/nginx.conf中使用了正確數量的worker_processes。這應該是等於CPU核心在命令波紋管的輸出量,(執行它在您的終端應用程序):

cat /proc/cpuinfo | grep processor

在我的情況下的結果示出了僅一個處理器

[email protected]:~# cat /proc/cpuinfo | grep processor 
processor : 0 
[email protected]:~# 

所以我的機器只有1個處理器可用,那麼我設置

[...] 
worker_processes 1; 
[...] 

我有意見最應該進行調整,再一次的重要組成部分,喲你應該研究並開始構建適合你工作/生產環境的自己的配置。我們沒有覆蓋任何緩存技術或通過ssl(https)安全連接提供服務,只是簡單的基本nginx配置。

user nginx; 

# Set the number of worker processes 
# You can also set it to "auto" to let Nginx decide the right number 
worker_processes 1; 
pid /var/run/nginx.pid; 

events { 
# Increase worker connections 
worker_connections 1024; 
# Accept() as many connections as possible 
# More information http://wiki.nginx.org/EventsModule 
multi_accept on; 

# Serve many clients with each thread (Linux) 
use epoll; 
} 

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"'; 

    # Let NGINX get the real client IP for its access logs 
    set_real_ip_from 127.0.0.1; 
    real_ip_header X-Forwarded-For; 

## 
# Basic Settings 
## 

    # Tweak TCP connections handling 
sendfile on; 
tcp_nopush on; 
tcp_nodelay on; 

    # Increase keepalive timeout 
keepalive_timeout 65; 

    # Reset timedout connections and free up some memory 
    reset_timedout_connection on; 

    # Other buffers/timeouts optimizations 
    #if you want to allow users to upload files with a bigger size, consider increasing client_max_body_size to whatever fits your needs 
    client_max_body_size 20m; 
    client_body_timeout 60; 
    client_header_timeout 60; 
    client_body_buffer_size 8K; 
    client_header_buffer_size 1k; 
    large_client_header_buffers 4 8k; 
    send_timeout 60; 
    reset_timedout_connection on; 
types_hash_max_size 2048; 
    # Hide Nginx version 
server_tokens off; 

server_names_hash_bucket_size 128; 
#server_name_in_redirect off; 

include /etc/nginx/mime.types; 
default_type application/octet-stream; 

## 
# Logging Settings 
## 
    # Disable access log to boost I/O on HDD 
    # Enabling access_log off; on the web server can save a lot of I/O as well as CPU power. 

access_log off; 
access_log /var/log/nginx/access.log; 
error_log /var/log/nginx/error.log; 

    # Log Format 

    log_format main '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; 

## 
# Gzip Settings 
## 
    # Enable GZIP compression to save bandwidth 
gzip on; 
gzip_disable "msie6"; 
gzip_vary on; 
gzip_proxied any; 
    gzip_comp_level 6; 
gzip_buffers 16 8k; 
gzip_http_version 1.1; 
    gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml; 


include /etc/nginx/conf.d/*.conf; 
include /etc/nginx/sites-enabled/*; 
} 

我希望它能幫助你開始。祝你好運。