Nginx默认配置
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
multi_accept on;
accept_mutex on;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
sendfile on;
gzip on;
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 {
alias html/vue1;
index index.html;
try_files $uri $uri/ /vue1/index.html;
}
location /vue2 {
alias html/vue2;
index index.html;
try_files $uri $uri/ /vue2/index.html;
}
location ^~/api/{
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_set_header Cookie $http_cookie;
proxy_set_header X_Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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;
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 -t
start nginx
nginx -s reload
nginx -s reopen
nginx -s stop
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;
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 记录客户端浏览器的相关信息
remote_addr
remote_port
server_addr
server_port
server_protocol
binary_remote_addr
connection
connection_request
proxy_protocol_addr
1proxy_protocol_port
request
uri
request_uri
scheme
request_method
request_length
args
arg_参数名
is_args
query_string
remote_user
host
http_user_agent
http_referer
http_via
http_x_forwarded_for
http_cookie
http_name
request_time
request_completion
server_name
https
request_filename
document_root
realpath_root
limit_rate
~为区分大小写,~*为不区分大小写
Vue项目打包路径不对问题
1.上路vue1项目路由前缀钱全部vue1,vue2项目路由前缀全部加vue2
2.Js,css路径不对(绝对路径改为相对路径)
publicPath: (process.env.NODE_ENV = 'production' ? './' : '/'),
3.css中引用的图片资源不对
Extract Css插件
publicPath改为'../../'