Nginx默认配置

674 阅读4分钟

Nginx默认配置

worker_processes 1;                 #允许生成的进程数,默认为1
error_log logs/error.log debug;      #制定日志路径级别

events {
  worker_connections 1024;          #最大链接数,512
  multi_accept on;                  #设置一个进程是否同时接受多个网络连接,默认off
  accept_mutex on;                  #设置网路连接序列化,防止惊群现象发送,默认on
}

http {
  include  mime.types;                           #文件扩展名与文件类型映射表
  default_type  application/octet-stream;        #默认文件类型/默认为text/plain
  keepalive_timeout 65;                          #链接超时时间,默认为75s
  sendfile on;                                   #允许sendfile方式传输文件,默认off
  gzip on;                                       #gzip压缩  
  gzip_min_length 10k;                           #文件压缩起始
  gzip_comp_level 2;                             #压缩级别
  gzip_types text/plain application/javascript application/x-javascript text/css 
             application/xml text/javascript image/jpeg image/gif image/png;
  gzip_vary on;
 
  server {
    listen 8009;                               #监听端口
    server_name localhost;                     #监听地址
    keepalive_requests 120;                    #单链接请求上限次数

    error_page 404    /404.html;   
    error_page 500 502 503 504 /50x.html;

    location /vue1 {                           #项目1的地址,http://localhost:8009/vue1访问
      alias html/vue1;                               #文件存放的目录,默认为html
      index index.html;                        #设置默认页
      try_files $uri $uri/ /vue1/index.html;        #后端支持hash变为history    
    }

    location /vue2 {                           #项目2的地址,http://localhost:8009/vue2访问
      alias html/vue2;
      index index.html;  
      try_files $uri $uri/ /vue2/index.html;
    }
    
    #接口代理
    location ^~/api/{
      proxy_pass http://localhost:3000/;          #代理服务器地址
      proxy_redirect off;                        #修改真实服务器的location/refresh的值
      proxy_set_header Cookie $http_cookie;      #设置cookie
      proxy_set_header X_Real_IP $remote_addr;   #真实服务器获取真实的Ip
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  #和上面组合获取真实Ip
      proxy_set_header X-Forwarded-Host $server_addr;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header Host $http_host;          #主机
      server_name_in_redirect on;     #开启或关闭server_name指定的虚拟主机发起的绝对重定向功能
      proxy_connect_timeout 3600;     #服务器链接超时时间
      proxy_read_timeout 3600;
      proxy_send_timeout 3600;      
    }
    location = /50x.html {
      root html;
    }
   }
}

Nginx常用命令

修改自己的配置并验证,重启nginx
cd /usr/local/nginx/sbin  //进入目录
./nginx -t                //验证配置
./nginx -s reload         //重启nginx


### nginx常用命令
nginx -t             //查看nginx进程当前使用的配置文件
start nginx          //启动nginx
nginx -s reload      //重新载入配置文件
nginx -s reopen      //重启nginx
nginx -s stop        //停止nginx

Tips:
nginx配置每次更改之后
1.nginx -t 检查配置是否正确
2.nginx -s reload 重新加载配置
3.ps -ef|grep nginx 确定nginx正常运行
4.nginx的bin文件路径/usr/local/nginx/sbin/nginx
可以在此文件路径下执行stop/reopen等命令

/usr/local/nginx/sbin/nginx -s reopen 重启
/usr/local/nginx/sbin/nginx -s stop 停止

Nginx去掉前缀

服务器正常接口:         http://114.132.166.16:3000/auth/login
前端axios的baseUrl:    http://114.132.166.16:8009/api
页面请求:              /auth/login

前端实际请求地址:       http://114.132.166.16:8009/api/auth/login 
指向:                  http://114.132.166.16:3000/auth/login
需求                   需要去除前缀/api

配置方式一:
location ^~/api/{
  proxy_pass http://localhost:3000/  #注意末尾加了反斜杠
}

配置方式二:
location ^~/api/{
  rewrite ^/api/(.*)/$1 break;       #通过rewrite去改写
  proxy_pass http://localhost:3000   #注意末尾没有反斜杠
}

Nginx重启方式

1.systemctl重启
systemctl restart nginx

2.service重启
service nginx restart

3.bin文件重启
/usr/sbin/nginx -s reload

4.杀死端口重启
ps -ef|grep nginx
kill 2072

Nginx常用变量讲解

### 常用变量
$remote_addr$http_x_forwarded_for   用以记录客户端的Ip地址
$remote_user                          用来记录客户端用户名称
$time_local                           用来记录访问时间与时区
$request                              记录请求的url与http协议
$status                               记录请求状态;成功是200
$body_bytes_sent                      记录发送给客户端文件主体内容大小
$http_referer                         记录从哪个页面链接访问过来的
$http_user_agent                      记录客户端浏览器的相关信息

### TCP连接变量
remote_addr //客户端IP地址
remote_port //客户端端口
server_addr //服务器IP地址
server_port //服务器端口
server_protocol //服务端协议
binary_remote_addr //二进制格式的客户端IP地址
connection //TCP连接的序号,递增
connection_request //TCP连接当前的请求数量
proxy_protocol_addr //若使用了proxy_protocol协议 则返回协议中地址 否则返回空
1proxy_protocol_port //若使用了proxy_protocol协议 则返回协议中端口 否则返回空

### HTTP请求变量
request //请求行信息
uri //请求的URL,不包含参数
request_uri //请求的URL,包含参数
scheme //协议名,http或https
request_method //请求方法
request_length //全部请求的长度,包括请求行、请求头、请求体
args //全部参数字符串
arg_参数名 //特定参数值
is_args //URL中有参数,则返回?;否则返回空
query_string //与args相同
remote_user //由HTTP Basic Authentication协议传入的用户名
host //先看请求行,再看请求体,最后找server_name
http_user_agent //用户浏览器
http_referer //从哪些链接过来的请求
http_via //经过一层代理服务器,添加对应代理服务器的信息
http_x_forwarded_for //获取用户真实IP
http_cookie //用户cookie
http_name //任意请求头字段

### 处理HTTP请求变量
request_time //处理请求已耗费的时间
request_completion //请求处理完成返回OK,否则返回空
server_name //匹配上请求的server_name值
https //若开启https,则返回on,否则返回空
request_filename //磁盘文件系统待访问文件的完整路径
document_root //由URL和root/alias规则生成的文件夹路径
realpath_root //将document_root中的软链接换成真实路径
limit_rate //返回响应时的速度上限值

~为区分大小写,~*为不区分大小写

Vue项目打包路径不对问题


1.上路vue1项目路由前缀钱全部vue1,vue2项目路由前缀全部加vue2

2.Js,css路径不对(绝对路径改为相对路径)
publicPath: (process.env.NODE_ENV = 'production' ? './' : '/'),

3.css中引用的图片资源不对
Extract Css插件
publicPath改为'../../'