Different ports of nginx are mapped to port 80, http and https are requested at the same time
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 matching rules
In order to achieve the expected function, you need to add multiple location matching rules to port 443 under the Nginx configuration file. The rules are described as follows:
mode | meaning |
---|---|
location = /uri | = Means exact match, only a complete match can take effect |
location ^~ /uri | ^~ prefix match the URL path, and before the regular. |
location ~ pattern | The beginning indicates case-sensitive regular matching |
location ~* pattern | The beginning indicates case-insensitive regular matching |
location /uri | Without any modifiers, it also means prefix matching, but after regular matching |
location / | General matching, any request that does not match other locations will be matched, which is equivalent to default in switch |
In the case of multiple location configurations, the matching order is:
1. Exact match first =
2. Second prefix matching^~
3. Followed by regular matching in the order in the file
4. Then match the prefix match without any modification.
5. Finally it is handed over / general matching
When a match is successful, stop the match and process the request according to the current match rule
Summary: So the key to the realization of requirements is to use different matching rules to map the corresponding ports.
https443 port configuration multi-port mapping
Under the configured port 443, continue to add location rules
# 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;
}
}
After saving the modification, proceed to the execution path of nginx, first test whether the configuration file is correct:
# /usr/local/nginx/sbin/nginx -t
If everything is normal, different service requests can be completed externally through https://server domain name/testA and https://server domain name/testB.
node background running
1. Use forever
1. Install forever: npm install forever -g
2. Start the service: service forever start
3. Use forever to start the js file: forever start index.js
4. Stop the js file: forever stop index.js
5. Start the js file and output the log file: forever start -l forever.log -o out.log -e err.log index.js
6. Restart the js file: forever restart app.js
7. View the running process:forever list
8. Stop the service under pid:forever stop [pid]
9. Stop all services:forever stopall
nginx settings cross-domain
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;
}}
Run the java package
Run the jar package
java -jar xxx.jar (当前目录下)
Find occupied ports
netstat -tln | grep 8000
result:
tcp 0 0 192.168.2.106:8000 0.0.0.0:* LISTEN
View the occupied port pid
sudo lsof -i:8082
result:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 10836 root 16u IPv4 3169457 0t0 TCP *:us-srv (LISTEN)
kill the process
sudo kill -9 (PID)