当前位置:首页 > nginx > 正文

nginx引用配置文件如何配置

  • nginx
  • 2024-05-03 12:24:42
  • 9440
本文目录一览

●^●如何配置Nginx做高可用
Nginx配置
Nginx配置主要是修改文件/usr/local/nginx/conf/nginx,conf
#配置用户和用户组
userwwwwww;
#工作数量进程,建议设置为CPU核心总数
worker_processes2;
#全局错误日志定义类型,日志级别从低到高为:debug|info|notice|warn|error|crit
error_loglogs/error.loginfo;
#记录主进程ID的文件
pid/usr/local/nginx/nginx.pid;
#最大进程理论上可以打开的文件描述符的数量。 该值应该是可以打开的最大文件数除以进程数。 但是,由于Nginx负载并不完全平衡,
#所以这个值最好对应于可以打开的最大文件数。 运行sysctl-a|grepfs.file以查看Linux文件描述符。
worker_rlimit_nofile65535;
#工作模式及最大连接数
事件{
#工作模式,Linux2.6以上使用epoll
useepoll;
#单进程允许的最大连接数
worker_connections65535;
#设置http服务器,利用其反向代理功能提供负载均衡支持
http{
#文件扩展名与文件类型关联表
includemime.types;
#默认文件类型
default_typeapplication/octet-stream;
#日志格式
log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
#accesslog记录哪些用户哪些页面、用户浏览器、IP等访问信息
access_loglogs/access.logmain;
#服务器名称哈希表大小
server_names_hash_bucket_size128;
#客户端请求头缓冲区大小。 默认情况下,Nginx使用“client_header_buffer_size”缓冲区来读取标头值。
#如果标头太大,则会使用“large_client_header_buffers”来读取。
#如果HTTPheader/cookie太小,则报400错误nginx400badrequest。
#如果超出缓冲区,则报HTTP414错误(URITooLong)。
#nginx接受最长的HTTP标头大小必须大于其中一个缓冲区,否则会报告400HTTP错误(BadRequest)。
client_header_buffer_size32k;
large_client_header_buffers432k;
#客户端请求正文大小
client_body_buffer_size8m;
#隐藏Ngnix版本号
server_tokensoff;
#忽略非法请求header
ignore_invalid_headerson;
#指定应启用除第一个error_page指令之外的其他error_pages。
recursive_error_pageson;
#当Nginx处理自己的内部重定向时,不要让Nginx默认为服务器名称设置中的第一个域名。
server_name_in_redirectoff;
#启用文件传输,一般应用程序应设置为“开”;如果有下载的应用程序,可以设置为“关闭”,以平衡网络I/O和磁盘I/O,减少系统负载
sendfileon;
#指示Nginx发送数据包所有头文件不是一个接一个地发送,而是一个接一个地发送。
tcp_nopushon;
#指示Nginx不要缓存数据,而是分段发送——如果需要及时发送数据,应为应用程序设置该属性,
#提交这样的小片段。 当获取数据信息时,无法立即获取返回值。
tcp_nodelayon;
#长连接超时时间,单位为秒
keepalive_timeout65;
#gzip模块设置:使用gzip压缩可以减少网站带宽消耗,提高访问速度。
gzipon;#打开gzip
gzip_min_length1k;#最小压缩大小
gzip_buffers416k;#压缩缓冲区
gzip_http_version1.0;#压缩版本
gzip_comp_level2;#压缩级别
gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;#压缩类型
#upstream用于负载均衡。 这里配置需要查询的服务器地址和端口号。 max_fails是允许失败的请求数。 默认值为1。
#weight为查询权重,可以根据不同的权重分配来平衡服务器的访问速率。
upstreamhostname{
server192.168.2.149:8080max_fails=0weight=1;
server192.168.1.9:8080max_fails=0weight=1;
#Host-配置
服务器{
#监听端口
listen80;
#域名
server_namehostname;
#Charset
charsetutf-8;
#单独的access_log文件
access_loglogs/192.168.2.149.access.logmain;
#反向代理配置,将所有对https://hostname的请求转发到上游定义的目标服务器。
location/{
#这里配置的域名必须与上游域名匹配才可以转发。
proxy_passhttps://hostname;
proxy_set_headerX-Real-IP$remote_addr;
#启用Nginxstatus监听页面
location/nginxstatus{
stub_statuson;
access_logon;
}
#错误页面
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}


至此,Nginx的基本负载均衡配置就完成了。 实验中部署了2个Tomcat,在浏览器中访问时返回了不同的结果,实际可以看到不同的返回结果。 Nginx配置文件的内容仍然需要检查。
ˇ▂ˇnginx配置文件详解
产品型号:ThinkpadE15
系统版本:centos8
nginx配置文件详细说明
#定义Nginx运行的用户及用户组
usernginxnginx;
#nginx数量进程数,建议设置等于CPU核心总数。
worker_processes8;
#全局错误日志定义类型,[debug|info|notice|warn|error|crit]
error_log/var/log/nginx/error.loginfo;
#进程文件
pid/var/run/nginx.pid;
#nginx进程打开的文件描述符的最大数量。 理论值应该是最大打开文件数(系统值ulimit-n)和nginx进程数划分,但是nginx分配请求不均匀,所以建议保持该值与ulimit-n一致。
worker_rlimit_nofile65535;
#设置http服务器
http
{
includemime.types;#文件扩展名与文件类型映射表
default_typeapplication/octet-stream;#默认文件类型
#charsetutf-8;#默认编码
server_names_hash_bucket_size128;#服务器名称哈希表大小
client_header_buffer_size32k;#上传文件大小限制
large_client_header_buffers464k;#设置设置请求buffer
client_max_body_size8m;#设置请求缓冲区
autoindexon;#启用目录列表访问,适合下载服务器,默认关闭。
tcp_nopushon;#防止网络阻塞
tcp_nodelayon;#防止网络阻塞
keepalive_timeout120;#长连接超时时间,单位为秒
#gzip模块设置
gzipon;#开启gzip压缩输出
gzip_min_length1k;#最小压缩文件大小
gzip_buffers416k;#压缩缓冲区
gzip_http_version1.0;#压缩版本(默认1.1,如果前端是Squid2.5请使用1.0)
gzip_comp_level2;#压缩级别
gzip_varyon;
#limit_zonecrawler$binary_remote_addr10m;#开启限制IP连接数时需要使用
#虚拟主机配置
server
{
#监听端口
listen80;
#可以有多个域名,以空格分隔
server_name.ha97.comha97.com;
indexindex.htmlindex.htmindex.php;
root/data//ha97;
位置~.*.(php|php5)?$
{
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
includefastcgi.conf;
}
#图片缓存时间设置
location~.*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires10d;
}
#JS和CSS缓存时间设置
location~.*.(js|css)?$
{
expires1h;
}
#定义该虚拟主机的访问日志
access_log/var/log/nginx/ha97access.logaccess;
#为“/”启用反向代理
location/{
proxy_passhttps://127.0.0.1:88;
proxy_redirectoff;
proxy_set_headerX-Real-IP$remote_addr;
#后端Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;


⊙▂⊙如何在Windows上配置并运行Nginx
Nginx(“enginex”)是一个高性能、轻量级的HTTPWeb服务器和反向代理服务器以及电子邮件IMAP/POP3/SMTP代理服务器。
Nginx是由俄罗斯程序员IgorSysoev为Rambler.ru网站开发的,Rambler.ru是该网站上访问量第二大的网站。 Igor在许可下将源代码转换为类似BSD的格式。
自发布四年以来,Nginx以其稳定性、丰富的功能集、示例配置文件和低系统资源消耗而闻名。 Nginx在性能和稳定性方面超越了Apache,这导致国内越来越多的网站使用Nginx作为Web服务器。
目前,Nginx应用于新浪、网易、腾讯等国内主要门户网站。 最近发现Nginx技术在国内越来越流行,越来越多的网站开始应用和部署Nginx。
1.首先下载Windows版本的nginx1.0.11。 下载软件包后,将nginx-nginx1.0.11.zip包解压到您的根目录下,并将目录名称更改为nginx。
然后执行以下操作:
cdnginx
startnginx
这样Nginx服务就会启动。 打开任务管理器,检查nginx.exe进程是否显示两个消耗大量系统资源的进程。 然后打开浏览器输入127.0.0.1即可显示Nginx欢迎页面,非常人性化
nginx-sstop//停止Nginx
nginx-sreload//重新加载配置文件
nginx-squit//退出nginx
2.下一步是配置nginx的conf文件。 这是我的配置:
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/errorloginfo;
#pidlogs/nginx.pid;
事件{
worker_connections1024;
http{
includemime.types;
default_typeapplication/八位字节-流;
log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
access_loglogs/access.logmain;
sendfileon;
#tcp_nopushon;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
服务器{
listen80;
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
位置~*软件/(.*)。 \.(jpg|rar)
{
expires21d;
rootd:/temp/;
access_logd:/temp/soft.3g.cn_nginx_s3gd-205.log;
proxy_storeon;
proxy_store_accessuser:rwgroup:rwall:rw;
proxy_temp_pathd:/temp/;
proxy_redirectoff;
proxy_set_headerHost112.25.10.140;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerx-forwarded-for$proxy_x_forwarded_for;
client_body_size1280k;
proxy_connect__Timeout900;
proxy_buffer_size40k;>proxy_buffers40320k;
proxy_busy_buffers_size640k;
proxy_temp_file_write_size640k;
if(!-e$request_filename)
{
proxy_passhttps://112.25.10.140:80;
}
location/{
roothtml;
indexindex.htmlindex.htm;
}
#error_page404/404.html;
#redirectservererrorpagestothestaticpage/50x.html
#
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
#proxythePHPscriptstoApachelisteningon127.0.0.1:80
#
#location~\.php${
#proxy_passhttps://127.0.0.1;
#}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
#
#location~\.php${
#roothtml;
#fastcgi_pass127.0.0.1:9000;
/>#fastcgi_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}
#denyaccessto.htaccessfiles,ifApache'sdocumentroot
#与nginx一致
#
#location~/\.ht{
#denyall;
#}
#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
#
#server{
#listen8000;
#listensomename:8080;
#server_namesomenamealiasanother.alias;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
#HTTPSserver
#
#server{
#列表443;
#server_namelocalhost;
#sslon;
#ssl_certificatecert.pem;
#ssl_certificate_keycert.key;
#ssl_session_timeout5m;
#ssl_protocolsSSLv2SSLv3TLSv1;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_pre fer_server_cipherson;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
}