本地配置案例
备份默认配置:
1、先将nginx.conf文件备份。
2、修改nginx.conf文件为以下形式:
# user指令在Windows上不生效,如果你制定具体用户和用户组会报小面警告
# nginx: [warn] "user" is not supported, ignored in D:\software\nginx-1.18.0/conf/nginx.conf:2
#user nobody;
worker_processes 1;
error_log logs/error.log warn;
pid nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"token_name": "$arg_testtoken" "$arg_TESTTOKEN" ';
access_log logs/access.log main;
sendfile off;
keepalive_timeout 65;
# 指定conf配置路径
include D:/Environment/nginx/nginx-1.8.1/conf/test_conf/*.conf;
}
#stream块和http块是两个不同的模块,stream不属于http模块
#注意:Nginx 从 1.9.0 版本才开始新增加 stream 模块,才支持 tcp端口,
#所以以下配置需要 Nginx 不小于 1.9.0 版本
include /usr/local/nginx/conf/tcp_conf/*.conf;
http 端口示例:
3、在 D:/Environment/nginx/nginx-1.8.1/conf/test_conf 目录下创建各种 http 服务:
(1)nginx_server.conf(nginx.conf默认配置文件中的 server 块)
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
(2)server01.conf
server {
# 监听端口()
listen 8011;
#server_name www.abc.com;
#如果访问不同的域名或不同的服务器,最好设置不同的日志文件,这样便于查错,如果不使用则是使用Nginx统一的日志,这样查起来太麻烦。
#access_log /var/log/nginx/99cdtop.access.log main;
#access_log on;
# 匹配任何请求,所有URI都是以“/”开始
location / {
# 前端项目的根路径
root D:/test/damp/;
error_page 405 =200 $uri;
index index.html;
# 添加改配置解决浏览器刷新页面报404问题
try_files $uri $uri/ /index.html;
}
# 匹配以 /v1 开头的请求
location /v1 {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
#proxy_set_header相关参数的设置,是为了把所有访问的真实主机信息、IP信息传送给代理服务器。(固定配置)
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://xx.xx.xx.xx:7003;
}
location /kibana {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Authorization "ApiKey alJaQ2xZa0JQTUR1alJ2WnBncHk6MFdlemhCaHFSMWlrUXQyM0Y3SkZwQQ==";
proxy_pass http://xx.xx.xx.xx:5601;
}
}
tcp端口示例:
注意:
Nginx 从 1.9.0 版本才开始新增加 stream 模块;
stream {
upstream nacos_client_grpc_server {
server ip:9841 weight=1 max_fails=1 fail_timeout=30s;
server ip:9843 weight=1 max_fails=1 fail_timeout=30s;
server ip:9845 weight=1 max_fails=1 fail_timeout=30s;
}
server {
listen 31000; # grpc方式对外暴露端口
proxy_connect_timeout 1s;
proxy_pass nacos_client_grpc_server; # 此处TCP端口转发代理
}
upstream nacos_client_grpc_server {
server ip:9841 weight=1 max_fails=1 fail_timeout=30s;
server ip:9843 weight=1 max_fails=1 fail_timeout=30s;
server ip:9845 weight=1 max_fails=1 fail_timeout=30s;
}
server {
listen 31000; # grpc方式对外暴露端口
proxy_connect_timeout 1s;
proxy_pass nacos_client_grpc_server; # 此处TCP端口转发代理
}
upstream nacos_server_grpc_server {
server ip:9849 weight=1 max_fails=1 fail_timeout=30s;
server ip:9849 weight=1 max_fails=1 fail_timeout=30s;
}
server {
listen 31001; # grpc方式对外暴露端口
proxy_connect_timeout 1s;
proxy_pass nacos_server_grpc_server; # 此处TCP端口转发代理
}
}
前端代理(前后端分离项目)
将前端打的 build 包中的文件放在 linux 服务器的某个路径下,如:/xxx/static/
打开 nginx ,在 http 块指定的配置路径下添加如下配置:配置文件名:xxx.conf,配置完成后使用
./nginx -s reload 命令刷新 nginx 配置。
server {
#前端访问的端口
listen 8011;
#前端访问的根路径
location / {
#前端访问的所有资源的路径
root /home/xxx/test/damp/;
error_page 405 =200 $uri;
index index.html;
}
#后端的根路径(一般不加)
location /v1 {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-Proto $scheme;
#后端服务的地址(ip + 端口)
#proxy_pass http://ip:7003;
proxy_pass http://ip:7003;
}
}

513

被折叠的 条评论
为什么被折叠?



