配置这玩意配了两天,公司网络卡是一回事,不懂正则又是一回事,最后连后台支不支持https都怀疑了,差点想用openssl生成个证书给后台,不过多搜索了一下,还是让我测通了,分享出来经验给大伙儿,不明白的评论
首先,不懂nginx 基本东东的童鞋,请花几分钟看看nginx的讲解,各种参数代表啥意思(我就搞懂了$request_uri 就解决了两天没搞出来的问题),还有稍微懂点正则表达式。实在不想搞懂的童鞋,最起码 rewrite 和 location 要搞懂,然后仿照我这个 nginx 来配置就可以了
想搞懂的童鞋,推荐个连接:
https://segmentfault.com/a/1190000002797606
(ps:里面有个$ 开头的各参数举例,例子有些是错的,特别是$request_uri,所以不要参考例子,只要知道个参数是干嘛的就行了,参数讲解是没错的)
继续
- 我的 nginx 配置的前情摘要:
- 我有个阿里域名 www.XXX.top,并且申请了证书,可以使用https
- 我配置了域名,指向我的阿里云服务器(windows server 2016)
- 我想要访问这个域名,就可以直接访问阿里云的80端口,后通过nginx解析到项目的前端(项目是前后端分离的)
- 后端我用的其他端口,但是由于https页面内不允许http请求,所以我的后端请求也要改成https
- 我要满足的需求:
- 访问 www.XXX.top,默认跳转
要实现的url跳转概览: www.XXX.top ---> https://www.XXX.top/AAA www.XXX.top/AAA ---> https://www.XXX.top/AAA www.XXX.top/BBB ---> https://www.XXX.top/BBB
- 后台也要通过 https 访问,原路径为 http://xxx.xxx.xxx.xxx:8090/demoService。。原来的用的是ip和端口访问的http请求
- 需要准备的东西:
基础的软件、后台的跨域设置、阿里云服务器域名指向以及端口开放就不用说了
nginx 的https 的配置,在阿里云里面有讲解,链接如下:
https://help.aliyun.com/knowledge_detail/95491.html?spm=a2c4g.11186623.2.9.13974c07MZ9izQ
记得server_name改成自己的,证书路径什么的都改成自己的,可参考:
https://blog.csdn.net/limingyue0312/article/details/79678869
还有个问题,如果你的服务器80端口被占用了,一定要先找出来干掉【心里头的疙瘩,不干掉不爽】,像我的云服务器,就莫名有个sqlserver的服务,占用了80端口,找了好几次才找出来,要掌握命令行这些找端口的命令:
- 找到pid,最后一列就是:netstat -ano | findstr "80"
- 通过找到的pid,查找任务进程谁占用了:tasklist|findstr ""【引号里放pid】
- 找到任务的进程id,去任务管理器里找是哪位,然后去服务里找到它,干掉,把启动属性选成手动启动以绝后患。
- 需要理解的例子:
如:
http://localhost:8888/test1/test2/test.php?param1='aaa'
$host:localhost
$server_port:8888
$request_uri:/test1/test2/test.php?param1='aaa'
$document_uri:/test1/test2/test.php 【和$uri 一样】
常用nginx 命令:【管理员身份命令行,跳到根目录(有nginx.exe 的目录)】
启动:start nginx.exe
进程结束后停止(可记录日志,有时候会很慢):nginx -s quit
强行终止(不记录日志):nginx -s stop
检查配置文件(配置文件写好后要用这个验证,通过后再启动,不然白搭):nginx -t
好了,可以上配置文件了,具体的说明,配置文件里都有:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
#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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# gzip on;
# gzip_min_length 1k;
# gzip_buffers 4 16k;
# gzip_http_version 1.0;
# gzip_comp_level 2;
# gzip_types text/plain application/x-javascript text/cssapplication/xml;
# gzip_vary on;
#配置虚拟主机
server {
#监听80端口
listen 80;
#你的域名,没有域名就写服务器ip就可以
server_name www.demo.top demo.top;
#这里判断request_uri 是否是空的【就是指host和端口号后面的所有东东(包括参数)】
#注意,即使是空的,也会有个 / 斜杠,所以要判断是否等于 /
if ( $request_uri != '/'){
#!!!!大坑,if 后面一定要有空格,括号前后也是,就是要隔开,不然nginx -t 报错,,,另外,没有else 只有if
rewrite ^/(.*) https://$server_name$request_uri? permanent;
}
#前面验证通过,后面就不会走了,因为跳到了https链接,会进下文配置的https接收里
rewrite ^/(.*) https://www.demo.top/AAA;
location / {
root html;
index index.html index.htm;
}
# 错误页重定向
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# #server里可以配置的其他东东参考,不要直接放开使用,按需拿到上面的server里用
# server{
##下面的 是要写在server里的,按需使用
# access_log logs/host.access.log main;
# location / {
# #域名www.test.com的请求全部转发到这里的服务器上
# proxy_pass http://back_server1;
# proxy_set_header Host $host;
# proxy_set_header X-Real-Ip $remote_addr;
# proxy_set_header X-Forwarded-For $remote_addr;
# #欢迎页面,按照从左到右的顺序查找页面
# index index.html index.htm front.html
# }
#
# location ~ \.php$ {
# proxy_pass http://127.0.0.1;
# }
#
# 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;
# }
#
# location ~ /\.ht {
# deny all;
# }
# }
#配置一个跳转,可在proxy_pass里使用
upstream demoService{
#你服务器的IP地址和后台的服务的端口
server xx.xx.xx.xx:8090;
}
# 配置 HTTPS server
server {
listen 443 ssl;#设置为on启用SSL功能。
server_name www.demo.top demo.top;# localhost修改为您证书绑定的域名。多个域名中间加空格
ssl_certificate ../cert/1775271_www.demo.top.pem;
ssl_certificate_key ../cert/1775271_www.demo.top.key;
#下面的东东是阿里云里的,就不深究了
ssl_session_cache shared:SSL:1m;#缓存大小
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; #修改protocols。
ssl_prefer_server_ciphers on;
#默认文件夹,项目在哪里你就写哪里,不然就要输入域名的时候自己在后面加文件夹名字
#这里html指的是nginx的文件夹,如果在html文件夹里新建个test文件夹,这里就可以直接写root html/test 就能直接指向test里的index了
location / {
root html;
index index.html index.htm;
}
#这里匹配域名host后面的第一个东东,用这个可以区分访问的是后台,比如访问后台是ip:port/demoService,这里区分demoService,就可以指向不同的端口了
location /demoService/ {
#允许的来源,自己后台跨域怎么设置的就自己设置,不设置也行,这里写域名
add_header 'Access-Control-Allow-Origin' 'https://www.demo.top';
#设置为true,允许ajax异步请求带cookie信息
add_header "Access-Control-Allow-Credentials" "true";
#设置为x-requested-with,content-type,允许ajax余部请求。
add_header "Access-Control-Allow-Headers" "x-requested-with,content-type";
#要跳转的地方,这里跳转到访问8090端口,也就是说端口号后面第一个目录是demoService的,都去访问8090后台端口
proxy_pass http://demoService;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 1024m;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffering on;
proxy_buffer_size 64k;
proxy_buffers 8 1m;
proxy_busy_buffers_size 2m;
#proxy_temp_file_write_size 0;
}
}
}
结束,如有疑问,请评论区讨论