nginx
安装 nginx
Apt update
Apt upgrade
sudo apt install nginx
systemctl status nginx
systemctl start nginx所有的 Nginx 配置文件都在/etc/nginx/目录下。
主要的 Nginx 配置文件是/etc/nginx/nginx.conf。
为每个域名创建一个独立的配置文件,便于维护服务器。你可以按照需要定义任意多的 block 文件。
Nginx 服务器配置文件被储存在/etc/nginx/sites-available目录下。在/etc/nginx/sites-enabled目录下的配置文件都将被 Nginx 使用。
最佳推荐是使用标准的命名方式。例如,如果你的域名是mydomain.com,那么配置文件应该被命名为/etc/nginx/sites-available/mydomain.com.conf
如果你在域名服务器配置块中有可重用的配置段,把这些配置段摘出来,做成一小段可重用的配置。
Nginx 日志文件(access.log 和 error.log)定位在/var/log/nginx/目录下。推荐为每个服务器配置块,配置一个不同的access和error。
你可以将你的网站根目录设置在任何你想要的地方。最常用的网站根目录位置包括:
/home/<user_name>/<site_name>
/var/www/<site_name>
/var/www/html/<site_name>
/opt/<site_name>host:没有端口的server_name :www.baidu.com
http_host:有端口的server_name :www.baidu.com
request_uri:server_name后面的部分 :/s?ie=utf-8&f=8&rsv_bp=1
问题二、注意alias和root配置的正确用法。
root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。。。当只有 ip 地址时,server_name 等于 ip 地址 server_name 192.168.1.100;
nginx 打开 gzip 功能,在安装目录 nginx.conf 中,将 gzip on;全部打开注释
nginx log 目录:**/var/log/**nginx/access.log。
nginx
vue nginx 404 问题
vue 项目打包,用 nginx 部署了,但是访问不到页面。history 模式,要设置访问所有路径都返回 index.html,要在容器里的 default.conf 增加一以下一句 try_files $uri $uri/ /index.html; 方法一是直接指定非 80 端口,拷一个.conf 文件到目录,监听该端口 方法二是先将容器中的文件拷贝出来 sudo docker cp clientdist:/etc/nginx/conf.d/default.conf /root/ 修改后再将容器中的文件拷贝回去 sudo docker cp /root/default.conf clientdist:/etc/nginx/conf.d/default.conf
MIME type of "text/html"问题
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
是二级目录问题,base 删除后,可见页面。第一次才需要,运行成功后,又会失败,还需后面补回。
前端项目打包子目录
1、添加配置vue.config.js 文件 静态资源路由 js,css 等路径
vue.config.js/module.exports/ publicPath: '/swj',
在 vue3 中,vite.config.ts defineConfig({ base: "/swj3",
2、同时在项目的 router 目录下 index.js ,mode: "history",或“hash”为 Vue 项目指定路由基本路径为 base: '/swj',
createRouter({ history: createWebHistory('/swj3')
3、在 nginx 配置文件中,(非 docker 模式)
location /swj { # 注意末尾不加'/',测试成功
alias /var/www/html/swj;
index index.html ;
try_files $uri $uri/ /swj/index.html;
}4、docker 模式(hash 模式成功,history 模式成功)
nginx
location /swj/ {
proxy_pass http://120.46.156.96:8012/;
}clientdist.conf
server {
listen 8011 default_server;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html ;
try_files $uri $uri/ /index.html;
}
}认证服务二级目录
location /auth/ {
# proxy_pass http://120.46.156.96:8001/;
proxy_pass http://authserver/;
}
注意末尾要加'/',api可用,但显示网页时静态资源末能加载。二级域名,无法访问
也要先在服务商处增加解释记录,否则无法访问,可增加泛解释。ssl 证书免费只能解释单个地址,且无泛解释此功能,只能申请多个来解决。
nginx 反向代理
location 是否以“/”结尾,在 ngnix 中 location 进行的是模糊匹配
没有“/”结尾时,location/abc/def 可以匹配 /abc/defghi 请求,也可以匹配 /abc/def/ghi 等
而有“/”结尾时,location/abc/def/ 不能匹配 /abc/defghi 请求,只能匹配 /abc/def/anything 这样的请求
proxy_pass 代理规则(是否以“/”结尾)
配置 proxy_pass 时,当在后面的 url 加上了 /,相当于是绝对路径,则 Nginx 不会把 location 中匹配的路径部分加入代理 uri。比如location /proxy/ {proxy_pass http://127.0.0.1/;},我们访问 http://IP/proxy/test.html,最终代理到 URL 是 http://127.0.0.1/test.html
如果配置 proxy_pass 时,后面没有 /,Nginx 则会把匹配的路径部分加入代理 uri。比如location /proxy/ {proxy_pass http://127.0.0.1;},我们访问 http://IP/proxy/test.html,最终代理到 URL 是 http://127.0.0.1/proxy/test.html在 etc/nginx/sites-enabled 中建立 shuze.net.conf
服务设置为 down,不参与负载均衡;
服务设置为 backup,当其他节点服务正常时,不对外提供服务,当其他节点服务挂掉之后才会自动启用此备份服务;
shuze localhost搭设
nginx
linux
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.25.1.tar.gz
tar -zxf nginx-1.25.1.tar.gz
cd nginx-1.25.1
./configure
make&&make install
nginx -v
cat /usr/local/nginx/conf/nginx.conf
location /swj { # 注意末尾不加'/',测试成功
alias /var/www/html/swj;
index index.html ;
try_files $uri $uri/ /swj/index.html;
}
nginx -s reload如果是windows,则下载nginx windows版,安装即可,要设置开机启动,注意shuze为localhost:8011端口