nginx从入门到入坟
1.nginx下载安装
下载地址:http://nginx.org/en/download.html
解压:tar -xf nginx-1.21.6.tar.gz
安装依赖:
yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
安装:./configure --prefix=/usr/local/nginx
make
make install
到此安装完成
注意:要关闭防火墙
systemctl stop firewalld.service
systemctl disable --now firewalld
2.启动命令
cd /usr/local/nginx/sbin
[root@sg-15 sbin]# pwd
/usr/local/nginx/sbin
[root@sg-15 sbin]# ./nginx
访问:浏览器输入192.168.0.215
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置,修改配置文件之后使用
3.服务脚本
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
//重新加载系统服务
systemctl daemon-reload
//开机启动nginx
systemctl enable nginx.service
//脚本启动
systemctl start nginx //启动
systemctl stop nginx //关闭
systemctl status nginx //状态
systemctl restart nginx //重启
systemctl reload nginx //重新加载配置文件
4.nginx目录说明
drwx------. 2 nobody root 6 4月 19 09:22 client_body_temp
drwxr-xr-x. 2 root root 4096 4月 19 09:20 conf
drwx------. 2 nobody root 6 4月 19 09:22 fastcgi_temp
drwxr-xr-x. 2 root root 40 4月 19 09:20 html
drwxr-xr-x. 2 root root 58 4月 19 10:16 logs
drwx------. 2 nobody root 6 4月 19 09:22 proxy_temp
drwxr-xr-x. 2 root root 19 4月 19 09:20 sbin
drwx------. 2 nobody root 6 4月 19 09:22 scgi_temp
drwx------. 2 nobody root 6 4月 19 09:22 uwsgi_temp
-------------------------------
conf:nginx配置文件目录
conf/nginx.conf:主配置文件
sbin:nginx主程序文件目录,启动需要用到
html:默认网页和静态资源
logs:日志目录
logs/access.log:记录访问日志
logs/error.log:记录访问出错日志(404等)
logs/nginx.pid:nginx启动的进程号
5.nginx配置文件
cat /usr/local/nginx/conf/nginx.conf
#user nobody; #定义Nginx运行的用户和用户组
worker_processes 1; # 启动进程数量,对应cpu数量最佳
#error_log logs/error.log; #全局错误日志定义类型
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #进程pid文件
#事件驱动
events {
worker_connections 1024; # 每一个进程可以创建多少个链接,默认1024
}
http {
include mime.types; #引入另外的配置文件,mime.types类型文件
default_type application/octet-stream; #默认的类型
#charset utf-8; #默认编码
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on; #数据零拷贝
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #保持链接超时时间
#gzip on;
# 一个server代表一个虚拟主机(vhost)
server {
listen 80; #监听端口号,不同的虚拟主机端口号不一样
server_name localhost; #当前主机的主机名字,可以配置域名
#charset koi8-r;
#access_log logs/host.access.log main;
# uri,http://www.baidu.com/xxooo/index.html,uri匹配域名之后的资源
location / {
root html; #html相对路径,相对/usr/local/nginx,安装路径
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #服务器错误时,转到/50x.html
location = /50x.html {
root html; # 从html目录下找50x.html
}
# 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$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
6.虚拟主机与域名配置和站点配置
本地修改hosts文件,添加:
192.168.0.215 www.jeff.com
浏览器测试:输入www.jeff.com,跳转到nginx
创建测试资源
mkdir /data
mkdir /data/image
mkdir /data/movie
vi /data/image/index.html:写入测试信息
vi /data/movie/index.html:写入测试信息
nginx最小配置
server_name可以配置多个域名
注意:从上至下依次匹配server_name,若都没有匹配上则默认第一个server
worker_processes 1; # 启动进程数量,对应内核数量最佳
#事件驱动
events {
worker_connections 1024; # 每一个进程可以创建多少个链接,默认1024
}
http {
include mime.types; #引入另外的配置文件,mime.types类型文件
default_type application/octet-stream; #默认的类型
sendfile on; #数据零拷贝
keepalive_timeout 65; #保持链接超时时间
# 虚拟主机1(vhost)
server {
listen 80; #监听端口号,不同的虚拟主机端口号不一样
server_name www.jeff.com www.jeff2.com; #当前主机的主机名字,可以配置域名
# http://www.jeff.com/
location / {
root /data/movie; #绝对路径
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html; #服务器错误时,转到/50x.html
location = /50x.html {
root html; # 从html目录下找50x.html
}
}
# 虚拟主机2(vhost)
server {
listen 81; #监听端口号,不同的虚拟主机端口号不一样
server_name www.jeff.com; #当前主机的主机名字,可以配置域名
# http://www.jeff.com:81/
location / {
root /data/image; #绝对路径
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html; #服务器错误时,转到/50x.html
location = /50x.html {
root html; # 从html目录下找50x.html
}
}
}
浏览器测试
6.1server_name配置规则
//1.完整匹配,域名可以有多个,用空格隔开
server_name www.jeff.com www.jeff2.com; //server_name可以配置多个域名
//2.通配符匹配
server_name *.jeff.com*
//3.正则匹配
server_name ~^[0-9]+\.jeff\.com$
7.反向代理负载均衡配置
正向代理:代理浏览器,请求的时候用不同的ip请求
反向代理:代理服务器,转发请求到不同的服务器,不同的服务
7.1负载均衡-proxy_pass配置
http://www.jeff.com/代理转发到http://www.baidu.com
注意:proxy_pass写到location里面,下面root等信息就不生效了
302临时重定向
proxy_pass:域名/ip都可以
nginx.conf
worker_processes 1; # 启动进程数量,对应内核数量最佳
#事件驱动
events {
worker_connections 1024; # 每一个进程可以创建多少个链接,默认1024
}
http {
include mime.types; #引入另外的配置文件,mime.types类型文件
default_type application/octet-stream; #默认的类型
sendfile on; #数据零拷贝
keepalive_timeout 65; #保持链接超时时间
# 虚拟主机1(vhost)
server {
listen 80; #监听端口号,不同的虚拟主机端口号不一样
server_name www.jeff.com www.jeff2.com; #当前主机的主机名字,可以配置域名
# http://www.jeff.com/
location / {
proxy_pass http://www.baidu.com;
#root /data/movie; #绝对路径
#index index.html index.htm;
}
error_page 500 502 503 504 /50x.html; #服务器错误时,转到/50x.html
location = /50x.html {
root html; # 从html目录下找50x.html
}
}
}
7.2负载均衡-weight权重配置/down下线配置/backup备用机器配置
# upstream配置与server同级
格式:
proxy_pass http://别名
# 轮训转发,不能放自己负载均衡器的nginx,默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。
upstream 别名{
server 192.168.0.216;
server 192.168.0.217;
}
# 配置权重,weight权重,比列:3:7
upstream 别名{
server 192.168.0.216:80 weight=3;
server 192.168.0.217:80 weight=7;
}
# down不参与负载均衡,(机器挂了),不建议使用down
upstream 别名{
server 192.168.0.216:80 weight=3;
server 192.168.0.217:80 weight=7 down;
}
# backup备用机器,当其他机器全部挂了之后就用backup备用机器
upstream 别名{
server 192.168.0.216:80 weight=3;
server 192.168.0.217:80 weight=7 backup;
}
down:表示当前的server暂时不参与负载
weight:默认为1.weight越大,负载的权重就越大。
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。
nginx.conf
worker_processes 1; # 启动进程数量,对应内核数量最佳
#事件驱动
events {
worker_connections 1024; # 每一个进程可以创建多少个链接,默认1024
}
http {
include mime.types; #引入另外的配置文件,mime.types类型文件
default_type application/octet-stream; #默认的类型
sendfile on; #数据零拷贝
keepalive_timeout 65; #保持链接超时时间
upstream httpds {
server 192.168.0.216:80 weight=3;
server 192.168.0.217:80 weight=7;
}
# 虚拟主机1(vhost)
server {
listen 80; #监听端口号,不同的虚拟主机端口号不一样
server_name www.jeff.com; #当前主机的主机名字,可以配置域名
# http://www.jeff.com/
location / {
proxy_pass http://httpds;
#root /data/movie; #绝对路径
#index index.html index.htm;
}
error_page 500 502 503 504 /50x.html; #服务器错误时,转到/50x.html
location = /50x.html {
root html; # 从html目录下找50x.html
}
}
}
7.3ip_hash配置
ip_hash:不常用,根据ip地址转发到同一台服务器,实现保持会话。
比如:登陆nginx转发到1号服务器,此时登陆会话在1号服务器。登陆成功之后nginx就转发到2号服务器。又需要重新登陆。因为nginx是轮训转发。
解决:使用ip_hash,相同的ip转发到同一台服务器。
问题:手机是移动的,ip是可变的,就不能解决此问题。所以不推荐使用ip_hash
最终解决:服务应该是无状态的,登陆token不保存在1号服务器,应该保存到数据库
#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
7.4least_conn配置
least_conn:最少连接访问,转发到最少连接到服务器
7.5负载均衡-动静分离
#所有静态文件由nginx直接读取不经过tomcat或resin
总结:静态资源从负载均衡服务器直接获取,动态的资源才从分发服务器获取
7.5.1-动静分离-location普通配置
# /usr/local/nginx/html/images
location /images {
}
# /usr/local/nginx/html/css
location /css {
expires 15d; #缓存有效期
}
# /usr/local/nginx/html/js
location /js {
}
nginx.conf:
worker_processes 1; # 启动进程数量,对应内核数量最佳
#事件驱动
events {
worker_connections 1024; # 每一个进程可以创建多少个链接,默认1024
}
http {
include mime.types; #引入另外的配置文件,mime.types类型文件
default_type application/octet-stream; #默认的类型
sendfile on; #数据零拷贝
keepalive_timeout 65; #保持链接超时时间
upstream httpds {
server 192.168.0.216:80;
server 192.168.0.217:80;
}
# 虚拟主机1(vhost)
server {
listen 80; #监听端口号,不同的虚拟主机端口号不一样
server_name www.jeff.com; #当前主机的主机名字,可以配置域名
location / {
proxy_pass http://httpds;
}
# /usr/local/nginx/html/images
location /images {
}
# /usr/local/nginx/html/css
location /css {
}
# /usr/local/nginx/html/js
location /js {
}
error_page 500 502 503 504 /50x.html; #服务器错误时,转到/50x.html
location = /50x.html {
root html; # 从html目录下找50x.html
}
}
}
7.5.2-动静分离-location正则匹配
#普通location:
location /js {
root html; #相对ngin安装路径
index index.html index.htm;
}
#正则匹配location:
location ~*/(js|images|css) {
}
#正则匹配location:
location ~ .*.(js|images|css)$ {
expires 15d; #缓存有效期
}
nginx.conf
worker_processes 1; # 启动进程数量,对应内核数量最佳
#事件驱动
events {
worker_connections 1024; # 每一个进程可以创建多少个链接,默认1024
}
http {
include mime.types; #引入另外的配置文件,mime.types类型文件
default_type application/octet-stream; #默认的类型
sendfile on; #数据零拷贝
keepalive_timeout 65; #保持链接超时时间
upstream httpds {
server 192.168.0.216:80;
server 192.168.0.217:80;
}
# 虚拟主机1(vhost)
server {
listen 80; #监听端口号,不同的虚拟主机端口号不一样
server_name www.jeff.com; #当前主机的主机名字,可以配置域名
location / {
proxy_pass http://httpds;
}
#正则匹配
location ~*/(js|images|css) {
}
error_page 500 502 503 504 /50x.html; #服务器错误时,转到/50x.html
location = /50x.html {
root html; # 从html目录下找50x.html
}
}
}
8.防盗链配置
valid_referers server_names:不带参数配置
valid_referers none server_names:检测Referer头域不存在的情况也可以访问
valid_referers blocked server_names:Referer不以“http://” 或 “https://” 开头也可以访问。
//参数说明:
none:检测 Referer 头域不存在的情况。
blocked:检测 Referer头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值不以“http://” 或 “https://” 开头。
server_names :设置一个或多个域名 ,检测Referer头域的值是否是这些URL中的某一个。
#浏览器header中referer:来源网址,设置防盗链
#检测浏览器referer是否为www.jeff.com
valid_referers www.jeff.com; if
($invalid_referer) {
return 403;
}
#正则匹配location:
location ~*/(js|images|css) {
valid_referers www.jeff.com; if
($invalid_referer) {
return 403;
}
root html; #相对ngin安装路径
index index.html index.htm;
}
# 403错误配置方式一,加上403
error_page 403 500 502 503 504 /50x.html;
# 403错误配置方式二
#正则匹配location:rewrite返回错误图片
location ~*/(js|images|css) {
valid_referers 192.168.0.215; if
($invalid_referer) {
rewrite ^/ /images/err.png break;
}
}
nginx.conf
worker_processes 1; # 启动进程数量,对应内核数量最佳
#事件驱动
events {
worker_connections 1024; # 每一个进程可以创建多少个链接,默认1024
}
http {
include mime.types; #引入另外的配置文件,mime.types类型文件
default_type application/octet-stream; #默认的类型
sendfile on; #数据零拷贝
keepalive_timeout 65; #保持链接超时时间
upstream httpds {
server 192.168.0.216:80;
server 192.168.0.217:80;
}
# 虚拟主机1(vhost)
server {
listen 80; #监听端口号,不同的虚拟主机端口号不一样
server_name www.jeff.com; #当前主机的主机名字,可以配置域名
location / {
proxy_pass http://httpds;
}
#正则匹配
location ~*/(js|images|css) {
valid_referers 192.168.0.215; if
($invalid_referer) {
return 403;
}
}
error_page 403 500 502 503 504 /50x.html; #服务器错误时,转到/50x.html
location = /50x.html {
root html; # 从html目录下找50x.html
}
}
}
9.nginx高可用keepalived配置
keepalived原理说明:一个虚拟ip绑定在nginx上,如果主nginx挂了,则ip换绑在其他nginx
# 安装keepalived
yum install keepalived
#启动
systemctl start keepalived
systemctl status keepalived
systemctl stop keepalived
#keepalived选举方式
priority优先级选举
主nginx
# 配置文件
vi /etc/keepalived/keepalived.conf
router_id nginx1 # router_id不能一样
vrrp_instance VI_1 {
state MASTER
interface eth0 # ip addr查看网卡名称
virtual_router_id 51
priority 100 #优先级,主备竞选时,数值大的是主
advert_int 1 #间隔检测时间
authentication { #认证配置
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.100 # 虚拟ip,以后外网访问的ip
}
}
备用nginx
# 配置文件
vi /etc/keepalived/keepalived.conf
router_id nginx2 # router_id不能一样
vrrp_instance VI_1 {
state BACKUP
interface eth0 # ip addr查看网卡名称
virtual_router_id 51
priority 50 #优先级,主备竞选时,数值大的是主
advert_int 1 #间隔检测时间
authentication { #认证配置
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.100
}
}
10.nginx域名证书配置https
第一步:阿里云申请域名,并绑定服务器
第二步:阿里云申请CA证书,并下载nginx的CA证书
第三步:下载的证书上传到nginx/conf目录下
第四步:nginx.conf添加一个server
nginx.conf
# 虚拟主机1(vhost)
server {
listen 443 ssl; #监听端口号,不同的虚拟主机端口号不一样
server_name loaclhost; #当前主机的主机名字,可以配置域名
ssl_certificate 123456_www.jeff.com.pem; #相对路径从conf目录中找
ssl_certificate_key 123456_www.jeff.com.key;
}
systemctl reload nginx //重新加载配置文件
再次访问时:http提示不安全,https安全
nginx从入门到入坟的更多相关文章
- 深度分析ReentrantLock源码及AQS源码,从入门到入坟,建议先收藏!
一.ReentrantLock与AQS简介 在Java5.0之前,在协调对共享对象的访问时可以使用的机制只有synchronized和volatile.Java5.0增加了一种新的机制:Reentra ...
- testlib.h从入门到入坟
学了这么久OI连个spj都不会写真是惭愧啊... 趁着没退役赶紧学一波吧 配置 github下载地址 我是直接暴力复制粘贴的.. 然后扔到MingW的目录里 直接引用就好啦 基本语法 引用testli ...
- SpringBoot整合SpringData JPA入门到入坟
首先创建一个SpringBoot项目,目录结构如下: 在pom.xml中添加jpa依赖,其它所需依赖自行添加 <dependency> <groupId>org.springf ...
- SrpingBoot入门到入坟04-配置文件
SpringBoot使用一个全局的配置文件,名称是固定,作用就是修改SpringBoot自动配置的默认值. *application.properties *application.yml 先看看两者 ...
- SrpingBoot入门到入坟03-基于idea快速创建SpringBoot应用
先前先创建Maven项目然后依照官方文档再然后编写主程序写业务逻辑代码才建立好SpringBoot项目,这样太过麻烦,IDE都支持快速创建,下面基于idea: 使用Spring Initializer ...
- SrpingBoot入门到入坟02-HelloWorld的细节和初始自动配置
关于SpringBoot的第一个HelloWorld的一些细节: 1.父项目 首先查看项目中的pom.xml文件 文件中有个父项目,点进去则: 它里面也有一个父项目,再点进去: 可以发现有很多的依赖版 ...
- SrpingBoot入门到入坟01-HelloWorld和SpringBoot打Jar包
第一个SpringBoot: 建立一个maven项目: 再pom.xml里面增加依赖: <?xml version="1.0" encoding="UTF-8&qu ...
- Nginx快速入门菜鸟笔记
Nginx快速入门-菜鸟笔记 1.编译安装nginx 编译安装nginx 必须先安装pcre库. (1)uname -a 确定环境 Linux localhost.localdomain 2.6. ...
- nginx配置入门
谢谢作者的分享精神,原文地址:http://www.nginx.cn/591.html nginx配置入门 之前的nginx配置是对nginx配置文件的具体含义进行讲解,不过对于nginx的新手可能一 ...
随机推荐
- Spring Boot 的配置文件有哪几种格式?它们有什么区别?
.properties 和 .yml,它们的区别主要是书写格式不同. 1).properties app.user.name = javastack 2).yml app: ...
- Spring 的优点?
(1)spring属于低侵入式设计,代码的污染极低: (2)spring的DI机制将对象之间的依赖关系交由框架处理,减低组件的耦合性: (3)Spring提供了AOP技术,支持将一些通用任务,如安全. ...
- Spring Cloud第一次请求报错问题
一.原因 我们在使用Spring Cloud的Ribbon或Feign来实现服务调用的时候,第一次请求经常会经常发生超时报错,而之后的调用就没有问题了.造成第一次服务调用出现失败的原因主要是Ribbo ...
- session与cookie的区别? 如果客户端禁止 cookie session 还能用吗?
一.session与cookie的区别 session:Session 是存放在服务器端的,类似于Session结构来存放用户数据,当浏览器 第一次发送请求时,服务器自动生成了一个Session和一个 ...
- 在java web工程中实现登入和安全验证
登入页面的话我们之前做过直接可以拿来用翻一翻之前的博客就可以找到 在这个基础上添加验证功能 代码如下: 1 package security; 2 /** 3 * @author 鐜嬭儨鍗? 4 */ ...
- (stm32学习总结)—对寄存器的理解
芯片里面有什么 我们看到的 STM32 芯片是已经封装好的成品,主要由内核和片上外设组成.若与电脑类比,内核与外设就如同电脑上的 CPU 与主板.内存.显卡.硬盘的关系.STM32F103 采用的是 ...
- 块级格式化上下文(BFC)
一.什么是BFC 具有BFC属性的元素也属于普通流定位方式,与普通容器没有什么区别,但是在功能上,具有BFC的元素可以看做是隔离了的独立容器,容器里面的元素不会在布局上影响到外面的元素,并且具有普通容 ...
- [译] 沙箱中的间谍 - 可行的 JavaScript 高速缓存区攻击
原文 The Spy in the Sandbox – Practical Cache Attacks in Javascript 相关论文可在 https://github.com/wyvernno ...
- 从零开始开发一款H5小游戏(二) 创造游戏世界,启动发条
本系列文章对应游戏代码已开源 Sinuous game 上一节介绍了canvas的基础用法,了解了游戏开发所要用到的API.这篇文章开始,我将介绍怎么运用这些API来完成各种各样的游戏效果.这个过程更 ...
- PAT A1035 Password
题目描述: To prepare for PAT, the judge sometimes has to generate random passwords for the users. The pr ...