本文共 4429 字,大约阅读时间需要 14 分钟。
作为高性能的反向代理服务器,Nginx 的配置对服务器性能和稳定性有着重要影响。本文将详细介绍 Nginx 配置文件的各项功能模块,并提供针对常见问题的解决方案。
user www www;
该配置项指定了 Nginx 运行的用户和用户组,通常建议将其设置为 www 用户,用于限制 Nginx 运行权限,防止配置文件被篡改。
worker_processes 8;worker_rlimit_nofile 65535;
ulimit -n 保持一致。events { use epoll; worker_connections 65535;} http { include mime.types; default_type application/octet-stream; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 64k; client_max_body_size 8m; sendfile on; autoindex on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 120;} fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;
gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;
server { listen 80; server_name ha97.com; index index.html index.htm index.php; root /data/www/ha97; location ~ .*.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 10d; } location ~ .*.(js|css)?$ { expires 1h; }} log_format access '$remote_addr - $remote_user [$time_local] "$request" "$status" $body_bytes_sent "$http_referer" "$http_user_agent" $http_x_forwarded_for';access_log ar/loginx/ha97access.log access;
location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file confpasswd;}location / { proxy_pass ""; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;} 默认 Nginx 端口为 80。如果需要将其重定向为 8000 等端口,可执行以下操作:
port_in_redirect off;server 块中,将 Host $host; 修改为 Host $host:端口号;解决方式来自:jingyan.baidu.com/article/6fb756ecbf4774241858fb9a.html
情况1:由于 Nginx 默认的 FastCGI 进程响应缓冲区太小造成的问题。
解决方法:
fastcgi_buffers 8 128k;情况2:PHP 环境配置问题。
解决方法:
php-fpm 的 max_children 为 30,提高处理效率。request_terminate_timeout 为 60 秒,防止进程被挂起。fastcgi_buffers 由 4 64k 改为 2 256k。针对通过 Nginx 转发的 JS 和 CSS,出现 net::ERR_INCOMPLETE_CHUNKED_ENCODING 的问题:
fastcgi_buffers 8 128k; fastcgi_buffer_size 128k;chown -R nobody:nobody proxy_temp/通过合理配置 Nginx,可以显著提升服务器性能和稳定性。本文详细介绍了 Nginx 配置的各项功能模块,并提供了针对常见问题的解决方案。
转载地址:http://mujfk.baihongyu.com/