https://www.howtoing.com/how-to-install-nginx-on-ubuntu-18-04-quickstart
如果使用的是阿里云服务器需要配置安全组https://www.cnblogs.com/themeth/p/10074593.html
注意:服务器开启防火墙后,不能使用远程连接
1.检测NGINX是否启动成功的命令: nginx -t
2.查看nginx的配置文件nginx.conf的命令 nginx -t
nginx的优点
1.高并发,高性能
2.可扩展性好
3.高可靠性
4.热部署,在不停止NGINX的情况下生机iNGINX
5.BSD.许可证
nginx的组成
1.NGINX的二进制可编辑模块,由各模块源码编译出来的.
2.nginx的配置文件,控制NGINX的行为
3.access.log nginx的访问日志.记录每一条http的请求
4.error.log 错误日志定位问题
nginx的应用场景
1.静态资源服务,通过本地文件系统提供服务,即将HTMLcss图片存储到本地
2.反向代理服务
1.缓存
2.负载均衡
3.API服务
NGINX的源码安装
博客地址: https://www.cnblogs.com/codedoge/p/10643636.html 其中有部分是错误的就是安装prce,这个博客中的这个下载不下来wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
由于我没有指定安装目录默认安装在 /usr/local/nginx, 未指定也是这个默认地址的。
我们进入到这个路径中,主要有这几个文件
这些文件主要包含四个:
conf:存放了Nginx的配置文件,其中nginx.conf是主配置文件,其他的都是配置Nginx相关的功能的,比如配置fastcgi使用fastcgi.conf和fastcgi_params两个文件。 .default结尾的都是默认的配置文件。方便将我们配置的.conf恢复到初始状态。
html:存放了Nginx服务器在运行过程中调用的一些html网页文件
logs:用来存放Nginx服务器的日志的,
sbin:只有一个nginx文件,就是Nginx服务器的主程序 ,nginx 的二进制文件
nginx的启动命令
#如果你没有配置环境变量,切换到配置文件/usr/local/nginx/sbin下 执行以下命令 重新加载配置: ./nginx -s reload 立刻停止: ./nginx -s stop 优雅的停止: ./nginx -s quit
启动 ./nginx 重新开始记录文件: ./nginx -s reopen 测试配置文件的语法 ./nginx -t
# 如果不是编译安装的NGINX
重新加载配置: nginx -s reload # 热部署在不停机的状态下更新NGINX的版本
NGINX常用的变量
# 请求开始对应的变量 remote_addr 客户端ip地址 remote_port 客户端端口 server_addr 服务器端ip地址 server_port 服务端端口 server_protocol 服务端协议 binary_remote_addr 二进制客户端ip地址 connection tcp连接的序号 connection_request tcp连接当前的数量 proxy_protocol 代理协议 proxy_protocol_port 代理端口号
#请求过程对应的变量 uri 请求的URL,不包含参数 request_uri 请求的url,包含地址 scheme 协议名,http还是https request_method 请求方法 request_length 全部的请求的长度包含请求头,请求行请求体 args 全部参数字符串和对应的值 pid=5555 args_参数名 特定参数值 is_args url中有参数,则返回?;否则返回空 query_string 与args相同 remote_user 由http basic Authentication协议传入args
#特殊的变量名 host Host的含义是表明要请求的主机名,因为nginx作为反向代理使用,而如果后端真是的服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,
如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败【默认反向代理服务器会向后端真实服务器发送请求,并且请求头中的host字段应为proxy_pass指令设置的服务器】。 http_user_agent 用户浏览器 http_referer 用户从哪个连接过来的 http_via 所经过的代理服务器信息 http_x_forwarded_for 获取用户真实的ip http_cookie 用户cookie


NGINX的配置文件
如果不使用源码 NGINX的配置文件有俩
1、nginx.conf 文件,路径为:/etc/nginx/nginx.conf
2、default 服务主机配置文件,路径为:/etc/nginx/sites-available/default
使用源码安装,不指定安装目录,默认安装在/usr/local/nginx 配置文件也在这里
配置文件的语法
原文链接:https://blog.csdn.net/weixin_44093297/article/details/90084938
https://www.cnblogs.com/qinyujie/p/8979464.html
1. 配置文件由四个指令块组成(http, upstream, server, location)
2. 指令块内容 以 " { " 开头 " } " 结尾,表示指令块配置到此结束, 指令块中配置指令以 " ; " 结尾. 指令与参数之间以 一个或者多个空格 做分隔.
3 .include 可以允许组合多个配置文件, 提升可维护性
4 ." # " 作为注释, 跟shell 语法一样 使用 " $ " 调用变量, 并支持正则.
nginx.conf
配置文件一共分为三个部分
1.main 全局设置
2.event块 事件模块设置
3.http块 http核心模块设置
#####第一部分全局块
#使用的用户和组 user www-data; #指定工作进程数(一般等于CPU总核数或总核数的两倍)处理并发的数量 worker_processes 4;
# 存放nginx错误日志的地方
#error_log logs/error.log #指定master_主进程的PID存放的路径 pid /run/nginx.pid; #指定worker子进程可以打开的最大文件句柄数 worker_rlimit_nofile 51200; events { #使用的网络I/O模型,linux推荐采用epoll模型,freebsd系统采用kqueue模型,推荐不指定让nginx自己选择,默认epoll use epoll; #work子进程能能够处理的最大并发连接数,默认1024,最大65535 worker_connections 51200;
#允许一个worker子进程处理过个请求 multi_accept on;
#打开work子进程互斥锁,提高子进程性能 默认为OFF
accept_mutex on :
} http { ## # 基础设置 ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; server_names_hash_bucket_size 128; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # 日志设置 ## #指定错误日志存放路径数, 错误日志记录的级别可选项为:[debug|info|notice|warn|error|crit] 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 2; gzip_buffers 4 16k; 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; ## # nginx-naxsi config ## # Uncomment it if you installed nginx-naxsi ## #include /etc/nginx/naxsi_core.rules; ## # nginx-passenger config ## # Uncomment it if you installed nginx-passenger ## #passenger_root /usr; #passenger_ruby /usr/bin/ruby; ## # 虚拟主机设置 ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
全局快
从配置文件开始到events之间的内容,主要设置一些影响NGINX服务器整体运行的配置命令,主要包括配置运行NGINX的用户组,允许生成的work进程数,进程pid存放的路径,指定文件描述符数量
event模块
event模块主要用来设置服务器与用户网络连接,比如设置网络连接数
http模块
http模块又包括全局快和server块.
http全局块:包括文件的引入\mime-type定义,日志的自定义,连接的超时时间,单链接的请求上限.
server块:和虚拟主机有密切的关系.每个http块可以多个server块,每个server块就相当于一个虚拟主机.每个server块又包含全局server块和多个location块.
全局server块:最常见的配置就是配置本机监听的端口和ip.
location块:这块的主要作用是基于NGINX服务器接收到的请求URL例如cctv.com/cctv5,对虚拟主机名称之外的字符串例如(/cctv5)进行匹配,对特殊的请求进行处理,地址定向,数据缓存和应答控制功能,还有许多第三方模块的配置也在这里进行.
server_name
的用法:server_name name1 name2 ; 可以跟多个域名,逐个去匹配直到找到该域名
四种写法:
server_name www.cctv.com server_name *.cctv.com server_name www.cctv.* server_name ~^www\.cctv.\*$ #~表示用正则表达式写
上边四种写法的匹配优先级
精确匹配》左侧通配符匹配》右侧通配符匹配》正则表达式匹配
root
root 指定网站的根目录
alias
与root的区别?
共同点都是用来指定网站的根目录 区别root会将定义路径与uri叠加,alias,只取定义路径,alias只能用在location下,root都可以
location /picture{ root /opt/nginx/html/picture } 当客户端请求:www.cctv.com/picture/1.jpg, 则对应在服务器上的磁盘映射为:/opt/nginx/html/picture/picture/1.jpg #### location /picture{ alias /opt/nginx/html/picture/ #注意使用alias末尾一定要加反斜线/ } 当客户端请求:www.cctv.com/picture/1.jpg,则对应在服务器上的磁盘映射为:/opt/nginx/html/picture/1.jpg #这种方式并不会叠加路径
location
通过指定模式来与客户端请求的URI相匹配

第一种不带任何符号:
server { server_name baidu.com; location /abc { …… } } 那么,如下是对的: http://baidu.com/abc http://baidu.com/abc?p1 http://baidu.com/abc/ http://baidu.com/abcde
第二种:精确匹配
server { server_name sish location = /abc { …… } } 那么,如下是对的: http://baidu.com/abc http://baidu.com/abc?p1 如下是错的: http://baidu.com/abc/ http://baidu.com/abcde
第三种按正则表达式(区分大小写)
server { server_name baidu.com; location ~ ^/abc$ { …… } } 那么,如下是对的: http://baidu.com/abc http://baidu.com/abc?p1=11&p2=22 如下是错的: http://baidu.com/ABC http://baidu.com/abc/ http://baidu.com/abcde
第四种正则表达式不区分大小写
server { server_name baidu.com; location ~* ^/abc$ { …… } } 那么,如下是对的: http://baidu.com/abc http://baidu..com/ABC http://baidu..com/abc?p1=11&p2=22 如下是错的: http://baidu..com/abc/ http://baidu..com/abcde
优先级:
= > ^~ > ~ > ~* > 不带任何符号
location 后边的URL有没有反斜线的区别
server { listen 80; server_name example.com; root /var/www/html; location /images/ { # 处理以 /images/ 开头的请求 alias /var/www/images/; } location /images { # 处理以 /images 开头但不包括 /images/ 的请求 alias /var/www/special_images/; } }
location /images/ { ... }
- 匹配
/images/及其下的所有子路径。 - 请求
/images/pic.jpg会被处理。 - 请求
/images/dir/pic.jpg会被处理。 - 请求
/images不会被处理。
location /images { ... }
- 匹配精确的
/images路径以及任何不带尾部斜杠的/images前缀。 - 请求
/images会被处理。 - 请求
/imagesanything会被处理。 - 请求
/images/不会被处理。 - 请求
/images/pic.jpg不会被处理。
return

rewrite
根据指定正则表达式匹配规则,重写url
rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分内容,重定向到replacement,结尾是flag标记。


示例:
rewrite ^(.*)$ /pages/maintain.html break; # 接收所有的URL请求并重定向到 maintain.html网站, 如果没有找到就返回403
if
if 条件句,true就会走括号中的内容,false什么也不会干
if (condition) { limit_rate 10k; break; }
注意NGINX中的if条件句中没后else用法,也就是说匹配到只能走括号中的代码,匹配不到啥都干不了

autoindex
开启目录浏览
Nginx默认是不允许列出整个目录的。如需此功能,打开nginx.conf文件或你要启用目录浏览虚拟主机的配置文件,在server或location 段里添加上autoindex on;来启用目录流量,下面会分情况进行说明。
另外Nginx的目录流量有两个比较有用的参数,可以根据自己的需求添加:
autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
改为on后,显示的文件时间为文件的服务器时间
#主机 server { listen 80; server_name www.demo.com; root /home/zxl/wwwroot/demo/; index index.php index.html index.htm; #如果没有可访问目录或文件 if (!-e $request_filename) { #将访问路径跳转至根目录下的index.php接受处理 rewrite ^/(.*)$ /index.php/$1 last; break; } #处理请求路径满足匹配 .php 的响应 # “ location ~ \.php ” == “ location ~ \.php($|/) ” #第一句的意思是如果请求字符中匹配到“ .php ”字符,就交给php解析器处理 #第二句的意思是如果请求字符中匹配到以“ .php ”字符为结尾的或 “ .php/ ” 字符,就交给php解析器处理 #推荐采纳第二句 location ~ \.php($|/) { #响应请求处理入口,使用php-fpm进行管理 fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #配置静态图片文件客户端缓存时间 location ~ .*\.(gif|jgp|jpeg|png|bmp|swf)$ { expires 30d; #30天 } #配置js、css文件客户端缓存时间 location ~ .*\.(js|css)?$ { expires 1h; #1小时 } #设置访问日志保存格式 #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 /var/log/nginx/access.log access; }
NGINX的操作命令
https://www.cnblogs.com/liulianghui/p/11569363.html
nginx反向代理实例一
实现效果:在浏览器中输入www.123.com自动跳转到我们的服务器中的某个网页
ubuntu的防火墙配置
https://blog.csdn.net/aa1209551258/article/details/81740298
查看防火墙的开放端口
#sudo ufw status Status: active To Action From -- ------ ---- Nginx Full ALLOW Anywhere Nginx HTTP ALLOW Anywhere 22/tcp ALLOW Anywhere Nginx Full (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6) 22/tcp (v6) ALLOW Anywhere (v
浙公网安备 33010602011771号