nginx 目录
如图:
- logs 可以记录 nginx 中的报错信息。
- html 存放打包好的项目资源。
- conf 中的 nginx.config 可以设置访问端口号和代理地址。
nginx 使用
- 下载 nginx
- 把打包好的项目目录放在 html 文件夹中
- 根据 nginx.config 中的端口号,打开浏览器用 http://localhost:端口号/#/index 访问
nginx.config 配置详解
nginx 配置样例
例如页面请求地址为http://www.sayhi.com/products/pageManage,则在nginx.conf中配置server如下:
server {
listen 80; #监听80的服务端口
server_name www.sayhi.com; #监听的域名
location /{
root emstatic;
index index.html;
location /products {
proxy_pass http://127.0.0.1:8103/zuul-product/product/manage;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Origin' '*';
}
}
根据域名www.sayhi.com和后缀/products匹配到server中的server_name和location后,将剩余url后缀/pageManage拼接到location中的配置项proxy_pass的url后通过网关访问服务器资源。
所以访问http://www.sayhi.com/products/pageManage时经过nginx配置后实际访问的地址为http://127.0.0.1:8103/zuul-product/product/manage/pageManage。
nginx.config文件
#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;
client_max_body_size 10m;
sendfile on;
keepalive_timeout 65;
root html;
index index.html index.hml;
# websocket配置所需
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 9111 ssl;
server_name localhost;
# server_name 10.8.3.225;
# server_name sso3.towatt.cn;
ssl on;
ssl_certificate towatt.cn.crt;
ssl_certificate_key towatt.cn.key;
# websocket配置所需
location /ws {
proxy_pass http://192.168.30.137:20501/backlog/data;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location / {
root html;
index index.html index.htm;
}
location /index/ {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /auth/ {
proxy_pass http://127.0.0.1:8081/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /activity/ {
proxy_pass http://127.0.0.1:8082/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /system/ {
proxy_pass http://127.0.0.1:8083/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /finance/ {
proxy_pass http://127.0.0.1:8084/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /manage/ {
proxy_pass http://127.0.0.1:8085/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /operation/ {
proxy_pass http://127.0.0.1:8086/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /trace/ {
proxy_pass http://127.0.0.1:8087/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /government/ {
proxy_pass http://127.0.0.1:8088/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /vip/ {
proxy_pass http://127.0.0.1:8089/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /tenement/ {
proxy_pass http://127.0.0.1:8090/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /station/ {
proxy_pass http://127.0.0.1:8092/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /cecbill/ {
proxy_pass http://127.0.0.1:8093/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /inspection/ {
proxy_pass http://127.0.0.1:8094/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /report/ {
proxy_pass http://127.0.0.1:8095/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /risk/ {
proxy_pass http://127.0.0.1:8096/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /backlog/ {
proxy_pass http://127.0.0.1:8097/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /mediacenter/ {
proxy_pass http://127.0.0.1:8095/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /sso/ {
proxy_pass http://sso/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
location /appmanage/ {
proxy_pass http://appmanage/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
upstream sso{
server 192.168.30.123:21017 weight=1 ;#server
}
upstream appmanage{
server 10.8.3.37:8086 weight=1 ; //后端服务的ip
}
}
标签:set,http,header,pass,nginx,proxy,使用,location
From: https://www.cnblogs.com/hxy--Tina/p/16899893.html