2

我的网站存在性能问题。我的配置是 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;
    }
}
4

2 回答 2

2

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

一些建议:

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

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

就分析器而言,我是NewRelic的忠实粉丝,他们的 Pro 级别在前 2 周是免费的(通常足够长以找到热点),基本性能信息永远免费。

于 2012-07-11T20:31:42.653 回答
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 应用程序)。因此,我已经启动并运行了一个没有任何缓存插件甚至 APC/memcached 方案的 WordPress 站点(仍在研究适合我需要的最佳方法),而且我总是有出色的性能。

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

那么让我们来看看吧……

调整 nginx

确定 Nginx worker_processes 和 worker_connections

我们可以将单线程 Worker 进程的数量配置为 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/*;
} 

我希望它可以帮助您入门。祝你好运。

于 2014-02-25T04:00:30.853 回答