2025年2月24日 星期一 甲辰(龙)年 腊月廿四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

关于nginx的linux命令 以及 基本配置文件的配置

时间:09-25来源:作者:点击数:45
文章目录

nginx介绍

反向代理,https,动静分离(web 服务),负载均衡 (反向代理),web 缓存

内存少,并发能力强(支持50,000 个并发)

一、常用

1.1 命令

  • # 重启
  • nginx -s reload
  • sudo systemctl restart nginx # 这个常用且适合清理缓存
  • # 启动
  • cd /usr/local/nginx/sbin/
  • ./nginx
  • start nginx
  • # 停止
  • ./nginx -s stop
  • # 安全退出,优雅退出
  • ./nginx -s quit
  • # 重新加载配置文件
  • ./nginx -s reload
  • # 查看配置文件是否错误
  • ./nginx -t
  • # 查看nginx进程
  • ps aux|grep nginx
  • # 开启防火墙
  • service firewalld start
  • # 关闭防火墙
  • service firewalld stop
  • # 查询端口是否开放
  • firewall-cmd --query-port=8080/tcp
  • # 开放80端口
  • firewall-cmd --permanent --add-port=80/tcp
  • # 移除端口
  • firewall-cmd --permanent --remove-port=8080/tcp

1.2设置开机自启动

  • vim /etc/rc.local
  • 然后在底部增加
  • /usr/local/nginx/sbin/nginx

1.3 配置文件结构

配置文件默认是放在/usr/local/nginx/conf/nginx.conf,配置文件中默认有三大块:全局块、events块、http块。

  • worker_processes 1;
  • events {
  • worker_connections 1024;
  • }
  • http {
  • include mime.types;
  • default_type application/octet-stream;
  • sendfile on;
  • keepalive_timeout 65;
  • server {
  • listen 80;
  • server_name localhost; ## 域名
  • location / {
  • root html;
  • index index.html index.htm;
  • }
  • error_page 500 502 503 504 /50x.html;
  • location = /50x.html {
  • root html;
  • }
  • }
  • }

我们主要修改http块:

  • 反向代理 server { location / {} }
  • 负载均衡 upstream 任意名{}

1.4 各个字段的含义

1.4.1 server下面
  1. listen 1401 ssl;
    listen [::]:1401 ssl;
  2. root /var/www/html;
  • root指令位于server块上下文中。 当选择用于服务请求的location块不包含自己的root指令时,将使用此root指令。
  •   这个比较重要。
  •   说明了/data/up1这个目录下面的文件是会被访问到的。
  •    root指令指定将用于搜索文件的根目录。
  •   为虚拟服务器定义了root指令。 它适用于不包括root指令的所有location块以显式重新定义根
  1. index index.html index.htm index.nginx-debian.html; # 指明虚拟主机的默认主页
  2. server_name eam.zhongyuntech.cn; # 填写服务器的域名
  • 5. location 可以在里面对请求做一些操作
1.4.1 server - location 下面
  • proxy_set_header X-Real-IP $remote_addr;
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  • proxy_set_header X-Forwarded-Proto $scheme;
  • proxy_set_header host $host;

上面是收集请求的来源,放进nginx的请求头中

  • proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响应超时时间;
  • proxy_read_timeout:连接成功后等候后端服务器响应时间其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间),调的比较小,会出现504,nginx使用proxy模块时,默认的读取超时时间是60s;;
  • proxy_send_timeout :后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据;
  • proxy_pass :

1.如果proxy_pass使用了URI,当传送请求到后端服务器时,规范化以后的请求路径与配置中的路径的匹配部分将被替换为指令中定义的URI(对应下文第一种情况)。

2.如果proxy_pass没有使用URI,传送到后端服务器的请求URI一般客户端发起的原始URI(下文第二种情况)。

例子:

访问地址:http://localhost/proxy/abc.html

proxy_pass使用了URI

  • location /proxy/ {
  • proxy_pass http://127.0.0.1:8080/;
  • }
  • 代理到:http://127.0.0.1:8080/abc.html
  • location /proxy/ {
  • proxy_pass http://127.0.0.1:8080;
  • }
  • 代理到:http://127.0.0.1:8080/proxy/abc.html
  • location /proxy/ {
  • proxy_pass http://127.0.0.1:8080/api/;
  • }
  • 代理到:http://127.0.0.1:8080/api/abc.html
  • location /proxy/ {
  • proxy_pass http://127.0.0.1:8080/api;
  • }
  • 代理到:http://127.0.0.1:8080/apiabc.html
  • location /proxy {
  • proxy_pass http://127.0.0.1:8080/api;
  • }
  • 代理到:http://127.0.0.1:8080/api/abc.html
  • location /proxy {
  • proxy_pass http://127.0.0.1:8080/;
  • }
  • 代理到:http://127.0.0.1:8080//abc.html
  • location /proxy {
  • proxy_pass http://127.0.0.1:8080;
  • }
  • 代理到:http://127.0.0.1:8080/proxy/abc.html

1.5 常使用操作

经常使用的配置修改

我们主要修改http块:

  • 反向代理 server { location / {} }
  • 负载均衡 upstream 任意名{}
  • http {
  • ......
  • # 负载均衡,确认服务器个数以及分配权重
  • upstream justtest {
  • server 127.0.0.1:8080 weight = 1;
  • server 127.0.0.1:8081 weight = 2;
  • }
  • # 反向代理
  • server{
  • listen 80;
  • server_name localhost;
  • location /{ # 访问80端口下的根目录就会走一下配置
  • root html;
  • index index.html
  • proxy_pass http://justtest ; # 负载均衡与 反向代理绑定
  • }
  • }
  • }

那重启nginx后,访问http://localhost/ 默认80端口就会进入 负载均衡那儿配置的网址

二、操作

基本上都是操作nginx.conf这个配置文件来实现的

2.1 解决跨域问题

用add_header指令,该指令可以用来添加一些头信息

  • location /xxx{
  • add_header ‘Access-Control-Allow-Origin’ *;
  • add_header ‘Access-Control-Allow-Credentials’ 'true';
  • add_header ‘Access-Control-Allow-Methods’ GET,POST,PUT,DELETE;
  • add_header ‘Access-Control-Allow-Headers’ *;
  • root /home/www/myweb;
  • index server1.html;
  • }

2.2 解决静态资源防盗链:

用valid_referers指令,如果在添加上域名或者IP地址,如果该值为1就返回403

  • location /xxx {
  • valid_referers none blocked www.baidu.* 127.0.0.1;
  • if ($invalid_referer){
  • # 返回403
  • return 403
  • # 如果让该图片显示其他默认图片
  • rewrite ^/ /images/图片名.png break;
  • }
  • root /usr/local/nginx/html;
  • }
  • # 如果不算这个域名,会重定向403
  • valid_referers *.goodysr.cn
  • if($invalid_referer){
  • return 403
  • }

2.3 Rewrite域名跳转:

访问xxx1、xxx2域名 会跳转到zong的域名下。

  • server {
  • listen 80;
  • server_name www.xxx1.com www.xxx2.cn;
  • rewrite ^(.*) http://www.zong.cn$1
  • # 也可以某个路径下进行跳转
  • location /user {
  • rewrite ^/user(.*)$ http://www.user.cn$1;
  • }
  • }

2.4 配置SSL(https)

步驟:生成证书

1.使用阿里云等平台购买: 或者使用openssl

购买证书,然后创建证书,进行绑定域名,然后审核通过,下载证书。

在服务器上某文件下(nginx/conf下),创建cert目录,存放所下载的证书文件。

  • server {
  • listen 80;
  • server_name 域名;
  • # 将http请求重定向https上
  • rewrite ^(.*)$ https://$host$1
  • location / {
  • root html;
  • index index.html index.htm;
  • }
  • }
  • server {
  • listen 443 ssl;
  • server_name 域名;
  • root html;
  • index index.html index.htm;
  • # 证书文件
  • ssl_certificate cert/xxx.pem;
  • ssl_certificate_key cert/xxx.key;
  • ssl_session_cache shared:SSL:1m;
  • ssl_session_timeout 5m;
  • # 加密规则
  • ssl_ciphers HIGH:!aNULL:!MD5;
  • # 表示使用TLS协议类型
  • ssl_protocols TLSv1 TLSv1.1 TLSv1.2
  • ssl_prefer_server_ciphers on;
  • location / {
  • root html;
  • index index.html index.htm;
  • }
  • }

案例:

SSL安全证书的上传

在nginx的conf目录下新建一个cert目录,并将这两个文件(nginx的ssl安全证书)上传到cert目录下

在这里插入图片描述
  • #nginx负载均衡的配置
  • upstream tomcatservers {
  • server 127.0.0.1:8080;
  • server 127.0.0.1:8081;
  • server 127.0.0.1:8082;
  • }
  • server {
  • listen 80;
  • server_name www.itbooking.net; #需要将yourdomain.com替换成证书绑定的域名。
  • rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
  • location / {
  • proxy_pass http://tomcatservers;
  • }
  • }
  • #以下属性中,以ssl开头的属性表示与证书配置有关。
  • server {
  • listen 443 ssl;
  • #配置HTTPS的默认访问端口为443。
  • #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
  • #如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
  • server_name www.itbooking.net; #需要将yourdomain.com替换成证书绑定的域名。
  • root html;
  • index index.html index.htm;
  • ssl_certificate cert/6179501_www.itbooking.net.pem; #需要将cert-file-name.pem替换成已上传的证书文件的名称。
  • ssl_certificate_key cert/6179501_www.itbooking.net.key; #需要将cert-file-name.key替换成已上传的证书密钥文件的名称。
  • ssl_session_timeout 5m;
  • ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  • #表示使用的加密套件的类型。
  • ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
  • ssl_prefer_server_ciphers on;
  • location / {
  • proxy_pass http://tomcatservers; # 反向代理
  • }
  • }
  • #以下属性中,以ssl开头的属性表示与证书配置有关。
  • #多域名配置,需要另一个ssl证书
  • server {
  • listen 443 ssl;
  • #配置HTTPS的默认访问端口为443。
  • #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
  • #如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
  • server_name api.itbooking.net; #需要将yourdomain.com替换成证书绑定的域名。
  • root html;
  • index index.html index.htm;
  • ssl_certificate cert/6184356_api.itbooking.net.pem; #需要将cert-file-name.pem替换成已上传的证书文件的名称。
  • ssl_certificate_key cert/6184356_api.itbooking.net.key; #需要将cert-file-name.key替换成已上传的证书密钥文件的名称。
  • ssl_session_timeout 5m;
  • ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  • #表示使用的加密套件的类型。
  • ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
  • ssl_prefer_server_ciphers on;
  • location / {
  • proxy_pass http://tomcatservers; # 反向代理
  • }
  • }

2.5 反向代理 & 负载均衡

我们主要修改http块:

- 反向代理 server { location / {} }

- 负载均衡 upstream 任意名{}

  • http {
  • ......
  • # 负载均衡,确认服务器个数以及分配权重
  • upstream justtest {
  • server 127.0.0.1:8080 weight = 1;
  • server 127.0.0.1:8081 weight = 2;
  • }
  • # 反向代理
  • server{
  • listen 80;
  • server_name localhost;
  • location /{ # 访问80端口下的根目录就会走一下配置
  • root html;
  • index index.html
  • proxy_pass http://justtest ; # 负载均衡与 反向代理绑定
  • # 假如只是想反向代理,比如80端口代理8080端口,
  • #那就proxy_pass http://127.0.0.1:8081
  • }
  • }
  • }

那重启nginx后,访问http://localhost/ 默认80端口就会进入justtest 负载均衡那儿配置的网址

2.6 配置缓存

  • http{
  • # 设置缓存根目录 levels缓存规则 keys_zone缓存名,缓存大小,1m=8000连接地址
  • # inactive缓存多久会被情况 max_size缓存空间最大空间
  • proxy_cache_path /usr/local/cache levels=2:1 keys_zone=缓存名:200m inactive=1d max_size=20g;
  • upstream backend{
  • server 192.168.200.146:8080;
  • }
  • server {
  • listen 8080;
  • server_name localhost;
  • location / {
  • # 设置不缓存 如果是js文件不进行缓存
  • if ($request_uri ~ /.*\.js$){
  • set $nocache 1;
  • }
  • proxy_no_cache $nocache $cookie_nocache $arg_nocache $arg_comment;
  • proxy_cache_bypass $nocache $cookie_nocache $arg_nocache $arg_comment;
  • proxy_cache 缓存名;
  • proxy_cache_key $scheme$proxy_host$request_uri;
  • # 当请求为5次才进行缓存
  • proxy_cache_min_uses 5;
  • # 当缓存状态码是200 ,缓存时长5天
  • proxy_cache_valid 200 5d;
  • # 当缓存状态码是404,缓存时长30s
  • proxy_cache_valid 404 30s;
  • proxy_cache_valid any 1m;
  • add_header nginx-cache"$upstream_cache_status";
  • proxy_pass http://backend;
  • }
  • }
  • }

删除对应的缓存目录:

rm -rf /usr/local/proxy_cache/…

2.6 静态文件的配置

在server里面配置

  • server {
  • listen 80;
  • server_name localhost;
  • location / {
  • root html;
  • index index.html index.htm;
  • }
  • location /image/ {
  • root /usr/local/myImage/;
  • autoindex on;
  • }
  • }

从上面的配置可以看出来 端口为80,server_name为localhost(写ip地址也可以)

location /image/ {

root /usr/local/myImage/;

autoindex on;

}

这个配置表示输入 localhost:80/image/ 时会访问本机的/usr/local/myImage/image/ 目录。

所以要新建/usr/local/myImage/image/ 目录,同时还要在nginx安装目录的html目录中新建一个 与 location中 image同名的image目录,虽然该目录里面什么也没有

在/usr/local/my Image/image/ 中我们放一张图片1.jpg上去,重启nginx服务,就可以通过 localhost:80/image/1.jpg访问了


校验配置是否有误如下:

  • [root@iZuf62zev3la2ivndnxra5Z servers]# nginx -t
  • nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  • nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

然后重启nginx服务

方式一:nginx -s reload

方式二:

在这里插入图片描述

然后访问服务器

https://www.itbooking.net/

2.7 搭建高可用Nginx集群:

准备两台Nginx机器,需要在这两台机器安装Keepalived,通过 VRRP 协议实现高可 用功能。

安装Keepalived

  • 步骤1:从官方网站下载keepalived,官网地址 https://keepalived.org/
  • 步骤2:将下载的资源上传到服务器
  • keepalived-2.0.20.tar.gz
  • 步骤3:创建keepalived目录
  • mkdir keepalived
  • 步骤4:将压缩文件进行解压缩,解压缩到指定的目录
  • tar -zxf keepalived-2.0.20.tar.gz -C keepalived/
  • 步骤5:对keepalived进行配置,编译和安装
  • cd keepalived/keepalived-2.0.20
  • ./configure --sysconf=/etc --prefix=/usr/local
  • make && make install

配置文件

一般在 /etc/keepalived/keepalived.conf ,我们进行配置

  • global_defs {
  • # 当keepalived发送切换时需要发email给具体的邮箱地址
  • notification_email {
  • xx1@xx1.com
  • xx2@xx2.com
  • }
  • # 设置发件人的邮箱信息
  • notification_email_from yy@xx3.com
  • # 指定smpt邮箱服务地址
  • smtp_server 192.168.200.1
  • smtp_connect_timeout 30
  • # 服务器的一个标识 A机器
  • router_id keepalivedA
  • vrrp_skip_check_adv_addr
  • vrrp_strict
  • vrrp_garp_interval 0
  • vrrp_gna_interval 0
  • }
  • vrrp_instance VI_1 {
  • # 两个值可选 MASTER主机 BACKUP从机
  • state MASTER
  • # 非抢占,这个参数只能用于state为backup
  • # 所以我们把state都设置成backup 让其通过priority来竞争,实现主机和从机
  • nopreempt
  • interface ens33
  • virtual_router_id 51
  • # 优先级高的将成为主机
  • priority 100
  • advert_int 1
  • authentication {
  • # 认证方式
  • auth_type PASS
  • # 指定认证使用的密码,最多8位
  • auth_pass 1111
  • }
  • virtual_ipaddress {
  • # 虚拟IP地址设置虚拟IP地址
  • 192.168.200.222
  • }
  • }

启用keepalived:

cd /usr/local/sbin

./keepalived

编写脚本实现自动切换

在keepalived配置文件中添加对应的配置像

  • gloval_defs{
  • ....
  • }
  • # ck_n为脚本名称
  • vrrp_script ck_n{
  • script "脚本位置"
  • interval 3 #执行时间间隔
  • weight -20 #动态调整vrrp_instance的优先级
  • }
  • vrrp_instance VI_1 {
  • ...
  • virtual_ipaddress {
  • 192.168.200.111
  • }
  • # 使用Shell脚本
  • track_script {
  • ck_n
  • }
  • }

内容:监听nginx运行状态,如果nginx启动失败,尝试再次启动,如果启动失败,关掉keepalived进程。

  • #!/bin/bash
  • num=`ps -C nginx --no-header | wc -l`
  • if [ $num -eq 0 ];then
  • /usr/local/nginx/sbin/nginx
  • sleep 2
  • if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then
  • killall keepalived
  • fi
  • fi

为脚本文件设置权限

chmod 755 ck_nginx.sh

2.8 正则匹配路径

  • = 表示精确匹配
  • ^~ 表示uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
  • ~ 正则匹配(区分大小写)
  • ~* 正则匹配(不区分大小写)
  • !~ 区分大小写不匹配
  • !~* 不区分大小写不匹配
  • / 任何请求都会匹配

注意要使用正则除了正则表达式,location 后还要加 ~ 相关表达式、 才能表示后面是正则表达式!!! 如下:

注意 :~ 表达式 和 正则表达式 中间有空格。

  • location [=|~|~*|^~] /uri/ { }

符号优先级:首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求

2.8.1示例

获取 location 中匹配的正则表达式匹配的字符串;

  1. 访问http://localhost/test/a.html,在本机寻找文件路径为/usr/local/html/a.html;
    $1表示正则表达式中的第一个括号内匹配的内容;
  • location ~ /test/(.*\.html)$ {
  • alias /usr/local/html/$1;
  • }

2.匹配MP_verify_****.txt文件,中间任意字符,并在/usr/share/nginx/file目录中寻找对应文件

  • # 微信授权文件通用匹配规则
  • location ~ (MP_verify_)*\.(txt)$ {
  • root /usr/share/nginx/file;
  • }
  1. 匹配/static开头路径,并在/data/product/static寻找对应文件
  • # 静态文件工程配置
  • location ^~ /static/ {
  • root /data/product/static;
  • index index.html index.htm;
  • }
  1. 匹配以.html 等结尾的
  • # 静态文件配置
  • location ~* \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
  • root /data/product/static/;
  • }

基础使用

  • location = / {
  • # 精确匹配 / ,主机名后面不能带任何字符串
  • [ 规则 A ]
  • }
  • location / {
  • # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
  • # 但是正则和最长字符串会优先匹配
  • [ 规则 B ]
  • }
  • location /documents/ {
  • # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
  • # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  • [ 规则 C ]
  • }
  • location ~ /documents/Abc {
  • # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
  • # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  • [ 规则 D ]
  • }
  • location ^~ /images/ {
  • # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
  • [ 规则 E ]
  • }
  • location ~* \.(gif|jpg|jpeg)$ {
  • # 匹配所有以 gif,jpg或jpeg 结尾的请求
  • # 然而,所有请求 /images/ 下的图片会被 [规则 E] 处理,因为 ^~ 优先级更高
  • [ 规则 F ]
  • }
  • location /images/ {
  • # 字符匹配到 /images/,继续往下,会发现 ^~ 存在
  • [ 规则 G ]
  • }
  • location /images/abc {
  • # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
  • [ 规则 H ]
  • }
  • location ~ /images/abc/ {
  • # 只有去掉 [规则 E] 才有效:先最长匹配 [规则 H] 开头的地址,继续往下搜索,匹配到这一条正则,采用
  • [ 规则 I ]
  • }
2.8.2 配置proxy_pass时路径拼接规则

在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/

  • 当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;
  • 如果没有/,则会把匹配的路径部分也给代理走
  • server {
  • listen 80;
  • server_name test.xxx.com;
  • location ^~ /abc {
  • proxy_set_header Host $host;
  • proxy_set_header X-Real-IP $remote_addr;
  • proxy_pass http://127.0.0.1:8081/;
  • }
  • }

加上/ 请求地址http://test.xxx.com/abc/index,会转发到http://127.0.0.1:8081/index

  • server {
  • listen 80;
  • server_name test.xxx.com;
  • location ^~ /abc {
  • proxy_set_header Host $host;
  • proxy_set_header X-Real-IP $remote_addr;
  • proxy_pass http://127.0.0.1:8081;
  • }
  • }

不加/ 请求地址http://test.xxx.com/abc/index,会转发到http://127.0.0.1:8081/abc/index

三、案例

在这里插入图片描述
  • user www-data;
  • worker_processes auto;
  • pid /run/nginx.pid;
  • include /etc/nginx/modules-enabled/*.conf;
  • events {
  • worker_connections 768;
  • # multi_accept on;
  • }
  • http {
  • ##
  • # Basic Settings
  • ##
  • sendfile on;
  • tcp_nopush on;
  • tcp_nodelay on;
  • keepalive_timeout 65;
  • types_hash_max_size 2048;
  • # server_tokens off;
  • # server_names_hash_bucket_size 64;
  • # server_name_in_redirect off;
  • include /etc/nginx/mime.types;
  • default_type application/octet-stream;
  • ##
  • # SSL Settings
  • ##
  • ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
  • ssl_prefer_server_ciphers on;
  • ##
  • # Logging Settings
  • ##
  • access_log /var/log/nginx/access.log;
  • error_log /var/log/nginx/error.log;
  • ##
  • # Gzip Settings
  • ##
  • gzip on;
  • # gzip_vary on;
  • # gzip_proxied any;
  • # gzip_comp_level 6;
  • # gzip_buffers 16 8k;
  • # gzip_http_version 1.1;
  • # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  • ##
  • # Virtual Host Configs
  • ##
  • include /etc/nginx/conf.d/*.conf;
  • include /etc/nginx/sites-enabled/*;
  • server {
  • listen 9326 default_server;
  • listen [::]:9326 default_server;
  • server_name 192.168.4.178;
  • root /home/zykj/retail/dist;
  • # Load configuration files for the default server block.
  • include /etc/nginx/default.d/*.conf;
  • location / {
  • try_files $uri $uri/ /index.html;#解决刷新访问404的问题
  • }
  • error_page 404 /404.html;
  • location = /40x.html {
  • }
  • error_page 500 502 503 504 /50x.html;
  • location = /50x.html {
  • }
  • location /newsell/ {
  • proxy_set_header X-Real-IP $remote_addr;
  • proxy_set_header Host $host;
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  • proxy_pass http://127.0.0.1:8326/;
  • }
  • }
  • server {
  • listen 9090;
  • #listen [::]:9090 default_server;
  • root /home/zykj/kyz/dist;
  • index index.html;
  • #server_name _;
  • #location / {
  • # First attempt to serve request as file, then
  • # as directory, then fall back to displaying a 404.
  • # try_files $uri $uri/ =404;
  • #}
  • location /web/ {
  • #proxy_read_timeout 120; # 秒
  • proxy_set_header X-Real-IP $remote_addr;
  • proxy_set_header Host $host;
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  • proxy_pass http://127.0.0.1:8090/web/;
  • }
  • }
  • server {
  • listen 9091;
  • #listen [::]:80;
  • root /home/zykj/zycx/dist;
  • index index.html;
  • location / {
  • try_files $uri $uri/ /index.html;
  • }
  • location /admin/ {
  • #proxy_read_timeout 120; # 秒
  • proxy_set_header X-Real-IP $remote_addr;
  • proxy_set_header Host $host;
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  • proxy_pass http://127.0.0.1:8091/admin/;
  • }
  • }
  • }
  • #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;
  • # }
  • #}

listen 端口号 … 和 listen 端口号 ssl … 的区别

  • listen 后的端口号 就是要监听的端口
  • 加ssl就是https的代理,不加就是http的;

加了ssl就得配置ssl

查看上面的ssl配置讲解;

  • ssl on;
  • ssl_certificate /home/zykj/nginx_https/1_zytravel.zhongyuntech.cn_bundle.crt; # 修改点1-服务器证书的位置-需要自己存放
  • ssl_certificate_key /home/zykj/nginx_https/2_zytravel.zhongyuntech.cn.key; # 修改点2-服务器证书钥匙位置-需要自己存放
  • ssl_session_timeout 5m;
  • ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  • ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  • ssl_prefer_server_ciphers on;

server-root 和location-root

采用的是就近原则;

server中有个root, location中也有root。当直接访问域名,后面什么都不加, 发现走的是location里面的root所指的路径。并没有走server中的root所指的路径;

通常存放静态资源,比如首页或者登陆页面的html和图标;

在这里插入图片描述

.index的作用

指定首页的html文件名,从root下找

在这里插入图片描述

server-location的关系

一对一的,多个端口监控就配置对个server-location

server里面有listen实现对端口的监听,而location就是实现负载均衡和反向代理(看上面的讲解);

这儿就举一个反向代理的例子:9090端口反向代理8090;

例子:ip:9090/web就会请求到ip:8090/web

在这里插入图片描述

四、常见问题

启动失败找不到pid文件

解决:

  • mkdir -p /var/run/nginx/
  • # 指定配置文件进行重启
  • nginx -C /usr/local/nginx/conf/nginx.conf
  • # 重启
  • nginx -s reload

配置环境变量:

  • vim /etc/profile
  • # 在文件末尾加上
  • export NGINX_HOME=/usr/local/nginx
  • export PATH=$NGINX_HOME/sbin:$PATH

设置开机自动启动:

  • vim /lib/systemd/system/nginx.service
  • # 在文件添加
  • [Unit]
  • #描述服务
  • Description=nginx
  • #描述服务类别
  • After=network.target
  • #服务运行参数的设置,注意【Service】的启动、重启、停止命令都要用绝对路径
  • [Service]
  • #后台运行的形式
  • Type=forking
  • #服务具体运行的命令
  • ExecStart=/usr/local/nginx/sbin/nginx
  • #重启命令
  • ExecReload=/usr/local/nginx/sbin/nginx -s reload
  • #停止命令
  • ExecStop=/usr/local/nginx/sbin/nginx -s quit
  • #表示给服务分配独立的临时空间
  • PrivateTmp=true
  • #运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
  • [Install]
  • WantedBy=multi-user.target
  • # 然后 修改文件权限
  • chmod 755 /usr/lib/systemd/system/nginx.service
  • # 设置开机自启动
  • systemctl enable nginx.service

访问404的问题

在server快里面加上

  • location / {
  • try_files $uri $uri/ /index.html;#解决刷新访问404的问题
  • }
在这里插入图片描述

请不要吝啬你发财的小手,点赞收藏评论,谢谢!

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐