配置nginx转发路由
Linux 上安装nginx
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
wget https://nginx.org/download/nginx-1.19.9.tar.gz
解压命令 tar -zxvf nginx-1.19.9.tar.gz
执行配置
./configure
#执行命令
make
#执行make install命令
make install
. 进入到nginx安装目录-配置文件 ```powershell cd /usr/local/nginx/conf
- 创建conf.d文件夹
mkdir conf.d
- 首先 更改nginx.conf文件内容
注意: include /usr/local/nginx/conf/conf.d/*.conf;
#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; include /usr/local/nginx/conf/conf.d/*.conf; default_type application/octet-stream; client_max_body_size 100m; proxy_connect_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; #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 0; keepalive_timeout 65; #gzip on; 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; #} } # server {
# listen 9898; # server_name 47.103.159.227:17000; # error_page 404 /index.html; #这个配置,预防页面刷新后跳转到404页面 # location / {
# if ($request_method = 'OPTIONS') { # add_header Access-Control-Allow-Origin *; # add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; # return 204; # } # root /data1/hecate-rent/dist; # index index.html index.htm; # error_page 404 /index.html; #这个配置,预防页面刷新后跳转到404页面 # location = /50x.html {
# root html; # } # } # } # another virtual host using mix of IP-, name-, and port-based configuration # #server {
# listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / {
# root html; # index index.html index.htm; # } #} # HTTPS server # #server {
# listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / {
# root html; # index index.html index.htm; # } #} }
- 在新建的conf.d 文件夹中新建需要配置的项目,比如创建 aa.conf
注意 :主要是防止页面跳转404
location / {
try_files $uri $uri/ /index.html;
add_header Access-Control-Allow-Origin *;
upstream backendTwo {
server ip:6009;
}
server {
listen 8013;
server_name ip;
try_files $uri $uri/ /index.html;
access_log /usr/local/nginx/logs/server.access.log;
location / {
try_files $uri $uri/ /index.html;
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
return 204;
}
root /data1/xxx/dist;
index index.html index.htm;
}
location ^~/api/ {
proxy_pass http://ip:6009/;
}
}
- 进入nginx sbin,重启nginx
cd /usr/local/nginx/sbin ./nginx -s reload
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231366.html原文链接:https://javaforall.net
