Nginx配置1.20.2版本 windows系统
nginx.conf设置全部代码 有证书的情况下 生效一个就可以 http的请求可以全部注释掉 只开启https的请求
#user nobody; #运行用户,默认即是nginx,可以不进行设置
worker_processes 8;#工作进程的个数,可以配置多个 #Nginx进程,一般设置为和CPU核数一样
#error_log logs/error.log; #错误日志存放目录
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程pid存放位置
#pid logs/nginx.pid;
events {
worker_connections 1024;#单个进程最大连接数(最大连接数 = 连接数 * 进程数)# 单个后台进程的最大并发数
}
http {
include mime.types;#文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型
#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;#nginx访问日志存放位置
#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件
#对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off
#以平衡磁盘与网络I/O处理速度,降低系统的负载
#注意:如果图片显示不正常把这个改成off
sendfile on;#开启高效传输模式
#tcp_nopush on;#减少网络报文段的数量
#keepalive_timeout 0;#保持连接的时间,也叫超时时间
#长连接超时时间,单位是秒
keepalive_timeout 65;
#启用Gizp压缩
#gzip on;
client_header_timeout 120s; #调大点
client_body_timeout 120s; #调大点
client_max_body_size 100m; #主要是这个参数,限制了上传文件大大小
client_body_buffer_size 256k;
#当前的Nginx的配置
# server {
#监听80端口,可以改成其他端口
# listen 80;
# server_name http://10.131.25.31;#当前服务的域名
#charset koi8-r;
#access_log logs/host.access.log main;
#访问页面
# location /{
# root html;#根目录 #服务默认启动目录
# index index.html index.htm; #默认访问文件
# try_files $uri $uri/ /index.html; #配置路由history模式不带#号的
# }
#访问Tomcat API 请求服务器的代理api
# location /api/ {
# #proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Real-Port $remote_port;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_pass https://10.131.25.31:8090/;
# }
#error_page 404 /404.html;# 配置404页面
# redirect server error pages to the static page /50x.html
#
# error_page 500 502 503 504 /50x.html;#错误状态码的显示页面,配置后需要重启
# location = /50x.html {
# root 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 8066 ssl; #如果配置端口号是443地址栏就会自动隐藏端口号
server_name http://10.131.25.31; #需要访问的域名,这里也不用加https;
ssl on;
ssl_certificate ../ssl/证书.crt; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
ssl_certificate_key ../ssl/证书.key; #这里是ssl 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;
try_files $uri $uri/ /index.html; #配置路由history模式不带#号的
}
#访问Tomcat API 请求服务器的代理api
location /api/ {
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://10.131.25.31:8099/;
}
}
}
注意: location /api/ 需要看前端是请求代理哪个代码的api 这里就需要跟着改 比如:location /prod-api/
nginx新建一个ssl文件夹放入证书
Egg
// egg/config/config.default.js
'use strict';
const path = require("path");
/**
* @param {Egg.EggAppInfo} appInfo app info
*/
module.exports = appInfo => {
config.cluster = {
https: {
key: path.join(__dirname, '../app/证书.key'), // https 证书绝对目录
cert: path.join(__dirname, '../app/证书.crt') // https 证书绝对目录
}
};
config.proxy = true;
}
最后把前后端所有的请求改成https。
history 模式作为路由模式时
在 Vue.js 中,当你使用 history 模式作为路由模式时,URL 将不再包含哈希(#)符号。这意味着服务器需要正确配置,以便在访问没有实际文件或目录的 URL 时,能够正确地回退到 index.html。
对于使用 nginx 作为服务器的场景,你可以按照以下步骤配置 nginx 来解决 Vue.js 的 history 模式路由问题:
打开 Nginx 配置文件:
通常,Nginx 的配置文件位于 /etc/nginx/nginx.conf 或 /etc/nginx/conf.d/default.conf。使用你喜欢的文本编辑器打开它。
2. 配置 Server Block:
找到你的 server block(通常是 http 块内部的 server 块),并添加以下配置:
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html; # 将所有请求都指向 index.html 页面
}
这个配置的作用是:
- 当请求一个 URL(例如
/some-path
)时,Nginx 首先会尝试查找该 URL 对应的实际文件或目录。 - 如果找不到,它会尝试查找该 URL 对应的目录。
- 如果仍然找不到,Nginx 会回退到
index.html
文件。 - try_files 指令会尝试将请求映射到对应的文件,如果找不到,则会将请求转发到 /index.html 页面,由前端来处理路由
纯接口跳转
前端页面请求带https请求数据,接口直接转http请求服务地址
server {
listen 8080 ssl; #如果配置端口号是443地址栏就会自动隐藏端口号
server_name http://10.131.25.31; #需要访问的域名,这里也不用加https;
ssl on;
ssl_certificate ../ssl/证书.crt; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
ssl_certificate_key ../ssl/证书.key; #这里是ssl key文件存放的绝对路径,根据自己的文件名称和路径来写
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://10.131.25.31:8099/;
}
}
nginx负载均衡配置 开启多个后台服务器
启动nginx命令
注意:目录不能有中文命名 不然不启动
start nginx
停止nginx命令
nginx.exe -s stop
重新载入Nginx
当配置信息修改,需要重新载入这些配置时使用此命令
nginx.exe -s reload