nginx不同端口映射到80端口,http和https同时请求
location / {
proxy_pass http://localhost:8090;
proxy_redirect default ;
}
location /xcx/{
proxy_pass http://localhost:8091/;
proxy_redirect default;
}
location / {
proxy_pass http://localhost:8090;
proxy_redirect default ;
}
location /xcx/{
proxy_pass http://localhost:8091/;
proxy_redirect default;
}
如果想通过 http://localhost:/xcx/a.html访问 http://localhost:8091/
则 proxy_pass http://localhost:8091/;
最后一定要加反斜杠,要不然会报404;
nginx匹配规则
为了实现预期功能,需要在Nginx配置文件下,对443端口添加多个location匹配规则,规则说明如下:
模式 | 含义 |
---|---|
location = /uri | = 表示精确匹配,只有完全匹配上才能生效 |
location ^~ /uri | ^~ 开头对URL路径进行前缀匹配,并且在正则之前。 |
location ~ pattern | 开头表示区分大小写的正则匹配 |
location ~* pattern | 开头表示不区分大小写的正则匹配 |
location /uri | 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后 |
location / | 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default |
多个 location 配置的情况下匹配顺序为:
1.首先精确匹配 =
2.其次前缀匹配 ^~
3.其次是按文件中顺序的正则匹配
4.然后匹配不带任何修饰的前缀匹配。
5.最后是交给 / 通用匹配
当有匹配成功时候,停止匹配,按当前匹配规则处理请求
总结:所以需求实现的关键,在于利用不同的匹配规则来映射相应的端口。
https443端口配置多端口映射
在已经配置好的443端口下,继续添加location规则
# HTTPS server
#
server {
listen 443;
server_name localhost;
ssl on;
root html;
index index.html index.htm;
*ssl证书配置规则*
location / {
proxy_pass *服务器域名*;
}
location /testA {
proxy_redirect off;
proxy_pass http://localhost:12345;
}
location /testB{
proxy_redirect off;
proxy_pass http://localhost:23456;
}
}
保存修改后,进行nginx的执行路径,先测试配置文件是否正确:
# /usr/local/nginx/sbin/nginx -t
如果一切正常,则即可在外部通过https://服务器域名/testA和https://服务器域名/testB来完成不同的服务请求。
node后台运行
1.利用forever
1、安装 forever: npm install forever -g
2、启动服务 : service forever start
3、使用 forever 启动 js 文件 : forever start index.js
4、停止 js 文件 : forever stop index.js
5、启动js文件并输出日志文件: forever start -l forever.log -o out.log -e err.log index.js
6、重启js文件 : forever restart app.js
7、查看正在运行的进程 :forever list
8、停止pid下的服务:forever stop [pid]
9、停止所有服务:forever stopall
nginx设置跨域
server {
listen 3000; //监听的本地端口
server_name localhost;
location /api { //匹配到/api开头的接口时,转发到下面的服务器地址
root html;
proxy_pass http://192.168.xxx.xxx:8080; //服务器地址
}
location =/ {
root html;
index index.htm index.html; //默认主页
}
# 所有静态请求都由nginx处理,存放目录为html
location ~ \.(htm|html|js|css|jpg|png|gif|eot|svg|ttf|woff|woff2)$ {
root html; //配置静态资源地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}}
运行java程序包
运行jar包
java -jar xxx.jar (当前目录下)
查找被占用的端口
netstat -tln | grep 8000
结果:
tcp 0 0 192.168.2.106:8000 0.0.0.0:* LISTEN
查看被占用的端口pid
sudo lsof -i:8082
结果:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 10836 root 16u IPv4 3169457 0t0 TCP *:us-srv (LISTEN)
kill掉该进程
sudo kill -9 (PID)