当前位置: 首页 > 后端技术 > Node.js

【运维】nginx服务器基础配置指南

时间:2023-04-04 01:10:50 Node.js

前言Nginx(enginex)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器安装yum安装源码安装yum安装以CentOS6.5为例设置yum源,vi等/百胜回购。d/nginx.repo[nginx]name=nginxrepobaseurl=http://nginx.org/packages/mainline/centos/6/x86_64/gpgcheck=0enabled=1setproxyvi/etc/yum.conf(如果是publicnetwork可以忽略)proxy=proxyIP命令安装sudoyuminstallnginx源码安装下载nginx源码wgethttp://nginx.org/download/nginx-1.13.12.tar.gztar-xvzfnginx-1.13.12.tar.gzcdnginx-1.13.12./configure--prefix=/usr/local/nginxmakesudomakeinstallmoduleextensioninstallation在安装nginx的时候,经常会有配置需求,如果我们要安装第三方模块,需要--添加要设置的模块。比如下面需要已知的第三方模块nginx-http-concat前端JS、CSS文件合并资源gitclonehttps://github.com/alibaba/nginx-http-concat.git#nginxcompile./configure--add-module=/path/to/nginx-http-concatmakesudomakeinstall源码重新编译有时候,我们需要在现有的nginx上添加一些扩展模块。然后我们需要通过对应版本的源码,重新编译configure,具体步骤如下:#老的nginx执行nginx-V/usr/local/nginx/sbin/nginx-V#相同下的源码nginx版本执行配置并附加新模块。/configure--prefix=/usr/local/nginx--add-module=/path/to/nginx-http-concat#编译make#备份旧的nginxcp/usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx.old#在objs目录下生成新的nginx覆盖旧的nginxcpobjs/nginx/usr/local/nginx/sbin/nginx#如果出现文件打开错误,运行如下mkdir-p/dev/shm/nginx_temp/client_body#testsudo/usr/local/nginx/sbin/nginx-t#顺利重启nginxsudo/usr/local/nginx/sbin/nginx-sreloadnginxconfiguration基本配置userwwwwww;#username用户组http{#http服务设置#引入多种mime类型includemime.types;#启用gzip压缩gzipon;#错误页面重定位error_page400401402403404405408410412413414415500501502503506=/error.html;#引入额外配置includevhosts/*.conf;}当虚拟主机配置用户请求主机为liylblog.com主机时,会访问根目录server中的资源{listen80;服务器名称liylblog.com;root/home/www/liyblog/html;}路由匹配设置server{listen80;服务器名称liylblog.com;根/home/www/liyblog/html;#最长优先匹配,所以/是最后一个匹配规则location/{#设置默认索引文件名indexindex.htmlindex.html;尝试_files$uri$uri/;}#以.php或.php/xxx结尾的路径,~表示区分大小写的位置~\.php($|/){#rule}#区分大小写,以/img/开头的资源路径会被匹配#/img/logo.png#/img/avatar.pnglocation^~/img/{#rules}#区分大小写,匹配/api资源位置^~=/api{f(!-e$request_filename){rewrite^/api/api/最后;}}}nginx配置负载均衡服务器{listen80;服务器名称liylblog.com;位置/{proxy_set_header主机$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-forwarded-For$proxy_add_x_forwarded_for;#重定向请求proxy_passhttp://proxy_stream;}}#多机代理,后面可能是其他web服务器upstreamproxy_stream{server192.168.1.1max_fails=3fail_timeout=30s;服务器192.168。1.2max_fails=3fail_timeout=30s;}反向代理PHP-FPM代理服务器{listen80;服务器名称liylblog.com;root/home/www/liyblog/html;访问日志/usr/local/nginx/logs/liylblog.access.log;error_log/usr/local/nginx/logs/liylblog.error.log;location/{indexindex.htmlindex.htmindex.php;if(!-e$request_filename){##ifrewrite^(.*)$/index.php?s=$1last;之后必须有一个空格;休息;}}#区分大小写,匹配以.php后缀或.php/结尾位置结尾的路径~\.php($|/){fastcgi_pass127.0.0.1:9000;fastcgi_indexindex.php;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi_paramPATH_INFO$fastcgi_script_name;fastcgi_split_path_info^(.+.?\)$.php)(fastcgi_paramPATH_INFO$fastcgi_path_info;client_max_body_size100M;fastcgi_connect_timeout300s;fastcgi_send_timeout300s;fastcgi_read_timeout300s;includefastcgi_params;}}Node.js代理日志;服务器。root/home/www/liyblog/html;access_log/usr/local/nginx/日志/liylblog.access.log;error_log/usr/local/nginx/logs/liylblog.error.log;location@proxy{proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_header主机proxy_set_headerX-NginX-Proxytrue;proxy_passhttp://localhost:7001;proxy_redirect关闭;proxy_http_version1.1;proxy_set_header升级$http_upgrade;proxy_set_header连接“升级”;proxy_set_headerX-Forwarded-Proto$scheme;$计划;}location/{autoindexon;indexindex.htmlindex.htm;#尝试访问地址eg,visit$root/foo,$root/foo/index.html,$root/foo/index.htm,$proxy/foo;try_files$uri$uri/@proxy;}}总结nginx是一个优秀的代理服务器,还有很多未知的知识亟待发现