Nginx性能问题

时间:2012-06-05 23:29:29

标签: performance wordpress nginx

我的网站存在性能问题。我的配置是在ubuntu 11.04上使用wordpress / nginx / php-fpm的1G VPS。瓶颈是浏览器等待服务器的第一个字节。在启动连接后,等待服务器的第一次响应需要4-6秒(网站是新的,它接收的流量非常低,每天约50-150次访问)。以下是我的nginx conf,我希望它可以帮助理解问题所在。我想知道这个配置是否有问题可以优化。此外,如果有人可以推荐我使用适合我的配置的分析/分析工具。

注意:我的用户名为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 个答案:

答案 0 :(得分:2)

看起来像一个Wordpress网站,我更倾向于认为它是一个性能问题,而不是nginx配置本身。

一些建议:

1 - 确保已安装并启用了APC

2 - 安装服务器端缓存插件(W3 Total Cache或Supercache)并将其配置为使用APC作为后备存储(并打开所有缓存层)

就个人资料而言,我是NewRelic的忠实粉丝,他们的专业级别在前2周免费(通常足够长时间找到热点),基本的表现信息永远免费。< / p>

答案 1 :(得分:0)

看来你的nginx配置确实有很大的改进空间。 Nginx在利用CPU和内存方面已经非常高效。但是,我们可以根据计划服务的工作负载类型调整几个参数。如果我们主要提供静态文件,我们希望我们的工作负载配置文件能够减少CPU占用,并且更加面向磁盘过程。实际上你的nginx.conf不应该是一个问题,只要nginx的本质是针对最大性能,但正如你所说,你根本没有获得nginx良好的性能。

我还运行一个1GB-1核心VPS运行一个新的LEMP安装(Ubuntu 14.04,nginx,MySQL,php5-fpm,没有别的人会考虑内存消耗,如cPanel,Zpanel和alikes,没有phpMyAdmin以及(我使用MySQL Workbench应用程序。)所以,我有一个WordPress网站启动并运行,没有任何缓存插件甚至APC / memcached方案(仍然在研究最适合我的需求的方法),我总是有一个很好的性能

无论如何,下面设置的nginx.conf仍然是一个非常基本的调整,以提高nginx的性能。这是我用来为我的网站提供服务的当前nginx.conf文件的副本。我在这里与大家分享一下作为参考。您可以根据自己的研究进一步调整它,但我相信您在尝试后会发现整体增强。

让我们来看看......

TUNING nginx

确定Nginx worker_processes和worker_connections

我们可以将单线程工作进程的数量配置为CPU核心数量的1.5到2倍,以利用磁盘带宽(IOP)。

确保在/etc/nginx/nginx.conf中使用正确数量的worker_processes。这应该等于命令输出中的CPU核心数量(在终端应用程序上执行):

cat /proc/cpuinfo | grep processor

在我的情况下,下面的结果只显示一个处理器

root@server1:~# cat /proc/cpuinfo | grep processor
processor : 0
root@server1:~#

所以我的机器只有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/*;
} 

我希望它可以帮助你开始。祝你好运。