Nginx配置详情记录

前端用,vue框架做开发;后台,用Spring boot开发。

因为用到Nginx,这里先分享一些基础。

正向代理: 客户端想访问一个服务器,访问不了,所以需要一个代理服务器,来访问目标服务器。
反向代理: 代理服务器为服务器做代理,部再服务器这边。

正向与反向的说法;代理在客户端时为正,而代理在服务器端时,针对用户来说就时反向了。

安装:

gcc --version
g++ --version

yum install -y gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/

#/usr/local/nginx 默认安装位置
./configure --prefix=/usr/local/nginx --with-http_ssl_module

make && make install


./nginx/sbin/nginx -t

#启动nginx
cd /usr/local/nginx/sbin
./nginx 

#pid错误
./nginx -c conf/nginx.conf

#环境变量配置
vi /etc/profile

PATH=$PATH:/usr/local/nginx/sbin
export PATH

source /etc/profile

部署文件nginx.conf:

配置静态资源访问 , 请求转发

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;
	

    server {
	
        listen       8090;			#监听端口的意思是: localhost:8090
        server_name  localhost;		#域名,这里是本地

        charset utf-8;

		
		location /upload/ {			# 这里的指: localhost:8090/upload/ 会指向 D:/upload/img/ 这个路径 
            alias D:/upload/img/; #开启目录浏览下载功能
            autoindex on;
        }
			
		location /test/ {			# proxy_pass,就是配置转发了, 这里指:localhost:8090/test/ 会转发到百度
			 proxy_pass   http://www.baidu.com/;
		}
		
		location /testrute/ {		# 这里指:localhost:8090/testrute/ 会转发 http://127.0.0.1:8082/ 这个地址。
									# 就是说, 比如你访问 http://localhost:8090/testrute/file/test, 实际上就想到于访问 http://localhost:8082/file/test , 你懂我的意思吧
			proxy_pass   http://127.0.0.1:8082/;
		}
		location /route/{		#根据url参数条件,配置转发路径

			if ( $query_string ~* ^(.*)ref_id=6 ){
				set $id $arg_ref_id;		#获取原url中参数 ref_id的值
				set $my_url  http://127.0.0.1:8083/file/test?ref_id=$id; #转发
				proxy_pass $my_url;
			}
			
		}
		
		
        error_page  404              /404.html; 
    }


}

down 当前server 不参与负载均衡
backup 备份服务器
max_fails 最大失败数
fail_timeout 经过max_fails后服务暂停的时间
max_conns 最大连接数

基本负载均衡: nginx 启动端口 8082


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


worker_rlimit_nofile 65535;
 
events {
        use epoll;
        worker_connections  102400;
        accept_mutex on;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
     
    upstream info-cluster {
		server 10.100.0.13:8083;
		server 10.100.0.13:8084;
		server 10.100.0.13:8085;
    } 
    server {
	
		listen       8082;			
        server_name  10.100.0.13;

        charset utf-8;
    
		location /info {
			proxy_pass http://info-cluster;
			add_header Access-Control-Allow-Origin *;  							
			proxy_next_upstream http_502 http_504 error timeout invalid_header; 
			proxy_set_header Host $host;							  			
			proxy_set_header X-Forwarded-For $remote_addr;						
			proxy_redirect default;
		}
		
        location = /50x.html {
            root   html;
        }
    }
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
     
    upstream tomcat-cluster {
     	server 127.0.0.1:8080 weight=1;
     	server 127.0.0.1:8081 weight=1 backup;
    } 
    server {
    
    	#配置HTTPS
        listen         443 ssl http2;
        server_name  www.xlkyy.cn;
        charset utf-8;
		ssl_certificate        /customer/nginx-1.16.1/cert/2766212_xlkyy.cn.pem;
		ssl_certificate_key    /customer/nginx-1.16.1/cert/2766212_xlkyy.cn.key;
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
		ssl_prefer_server_ciphers on;
		ssl_session_cache shared:SSL:10m;
		ssl_session_timeout 10m;
		
        #........
        location / {															 #配置路由
            root   /user/uccn;													 #跟目录
            index  index.html index.htm;
        }

		#这里注意,配置https,要先 安装ssl模块 ../configure --with-http_ssl_module
		location /officialWebsite {
			proxy_pass http://tomcat-cluster;
			add_header Access-Control-Allow-Origin *;  							 #表示服务器可以接受所有的请求源(Origin),即接受所有跨域的请求
			proxy_next_upstream http_502 http_504 error timeout invalid_header;  #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
			proxy_set_header Host $host;							  			 #原http请求的Header中的Host字段也放到转发的请求里
			proxy_set_header X-Forwarded-For $remote_addr;						 #XFF头,它代表客户端,也就是HTTP的请求端真实的IP,配置代理后保证网站的web服务器能获取到真实IP
			proxy_redirect default;
		}
		
        location = /50x.html {
            root   html;
        }
    }
}

配置前端项目:
配置前端

配置后台项目: 部署的是Spring boot的项目jar包。start-xlkOffical,是项目的指定命令:
nohup java -jar -Djava.net.preferIPv4Stack=true xlkOffical-0.0.1-SNAPSHOT.jar --server.port=8081 > /user/jar/xlkOffical.log &
后端项目部署

如果,在服务器原来还有其它的 tmocat 在运行。配置如下:
其它tomcat

常用指令:

netstat -ntlp	查询端口监听;

ps -ef|grep java

ps -ef|grep nginx

nginx -s reload  重启nginx

/sbin目录下, ./nginx -V 查询版本号以及配置参数

ls -aF 查询当前目录下文件,包含隐藏文件

cd .well-know/ 跳转隐藏目录

rpm -qa | grep ssl 查询rpm是否安装ssl  -qa query ;grep 文本搜索

nginx安装目录下: ./configure --help

带参数编辑nginx:./configure --prefix = /opt/nginx/1.16.1/ --with-http_ssl-module

vi objs/Makefile,修改 -00-02级别2

文件复制 : cp /usr/local/nginx/conf/nginx.conf /opt/nginx/1.16.1/conf/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值