nginx常用配置说明

总nginx配置

nginx.conf

#user  nobody;
worker_processes  2;

#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;

    #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       8020;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       
    }
  # 引入vhost 目录下的所有.conf文件
    include vhost/*.conf;

}
分包nginx配置,多个服务分文件管理

vhost/ry.conf 文件

前端访问地址是 http://10.128.51.43:8086

#也可以使用这种统一变量替换
# upstream ryback {
  #server xxxxx(ip地址)
# }

server {
	listen       8086;
	server_name  localhost;
   # 设置变量替换 index.html
	location @router {
                rewrite ^.*$ /index.html last;
        }

	location / {
	             #防止前端用webHistory路由刷新404
                 try_files $uri $uri/ @router;
                 #try_files $uri $uri/ /ui/index.html;
                 #alias  /data/ui/ui/;
                 #root  /data/ui/;
                 # 前端在服务器的地址
                 root  /data/ui/ui/;
                 index  index.html  index.htm;
        }

   # 这个前缀匹配是前端加的,和后端无关
	 location /ryapi {
	       rewrite ^/ryapi/(.*)$ /$1 break;
	       #后端服务地址
            proxy_pass http://10.128.51.43:8081;
            #proxy_pass http://ryback;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        }

  }
分包nginx配置

vhost/smart.conf

前端访问地址:10.123.54.43:(80端口可以不写)

upstream backend {
  server 10.123.54.43:9082;
  }
 

 server {
	listen       80;
	server_name  localhost;
  
	location @router {
                rewrite ^.*$ /index.html last;
        }
	location / {
	  # 解决前端刷新404
        try_files $uri $uri/ @router;
		#root   /data/ui;
		// 前端地址
		root   /data/ui/smart-main-ui/;
		#alias   /data/ui/smart-main-ui;
		index  index.html index.htm;
	}
  #第二个前端项目访问地址 ip/form-design,这里就不能用root一步到位了,需要使用try_files 后面跟着    # 根目录后面的名字
       location /form-design {
                 #try_files $uri $uri/ @router;
                 try_files $uri $uri/ /smart-form-design-ui/index.html;
                 #alias  /data/ui/smart-webform-ui/;
                 root  /data/ui/;
                 index  index.html  index.htm;
                 error_page 405 =200  $request_uri;

        }

	# 这个前缀匹配是前端加的,和后端无关
	 location /main {
	      # 转发到后端真实地址
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

  }
相关推荐
存储服务专家StorageExpert10 分钟前
DELL SC compellent存储的四种访问方式
运维·服务器·存储维护·emc存储
大G哥1 小时前
记一次K8S 环境应用nginx stable-alpine 解析内部域名失败排查思路
运维·nginx·云原生·容器·kubernetes
妍妍的宝贝1 小时前
k8s 中微服务之 MetailLB 搭配 ingress-nginx 实现七层负载
nginx·微服务·kubernetes
醉颜凉1 小时前
银河麒麟桌面操作系统修改默认Shell为Bash
运维·服务器·开发语言·bash·kylin·国产化·银河麒麟操作系统
苦逼IT运维2 小时前
YUM 源与 APT 源的详解及使用指南
linux·运维·ubuntu·centos·devops
仍有未知等待探索2 小时前
Linux 传输层UDP
linux·运维·udp
zeruns8023 小时前
如何搭建自己的域名邮箱服务器?Poste.io邮箱服务器搭建教程,Linux+Docker搭建邮件服务器的教程
linux·运维·服务器·docker·网站
北城青3 小时前
WebRTC Connection Negotiate解决
运维·服务器·webrtc
疯狂的大狗3 小时前
docker进入正在运行的容器,exit后的比较
运维·docker·容器
XY.散人3 小时前
初识Linux · 文件(1)
linux·运维·服务器