ubuntu Nginx安装和配置,负载均衡

持续更新中…

nginx常用命令:
nginx -t 检查conf文件
nginx -s stop 停止
nginx -s start 启动
nginx -s reload 软启动

日常启动报错:
nginx: invalid option: "-s start" 
nginx: [emerg] open() "/usr/local/etc/nginx/nginx.conf" failed (2: No such file or directory)

以上情况这样启动:
nginx -c /etc/nginx/nginx.conf

1.安装编译环境

//一键安装上面四个依赖(centos系统)
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
// ubuntu 系统
// 安装gcc g++的依赖库
apt-get install build-essential
apt-get install libtool
// 安装pcre依赖库
apt-get update
apt-get install libpcre3 libpcre3-dev
// 安装zlib依赖库
apt-get install zlib1g-dev
// 安装SSL依赖库  为配置域名做准备
apt-get install openssl
apt-get install libssl-dev

2.下载并解压安装包

//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget wget http://nginx.org/download/nginx-1.17.7.tar.gz
tar -xvf nginx-1.17.7.tar.gz

3.安装nginx

//进入nginx目录
cd /usr/local/nginx/nginx-1.13.7
//执行命令  后面的是nginx运行地址,也可不加默认就是/usr/local/nginx
// --with-http_ssl_module 这个是安装ssl所需要的模块
./configure  --prefix=/usr/local/nginx --with-http_ssl_module
//  ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module    看的别人的没研究是什么意思

//执行make命令
make
//执行make install命令
make install
常见错误参考此博客:https://blog.csdn.net/qq_37230094/article/details/82850343

4.运行nginx

//进入nginx运行目录
/usr/local/nginx/sbin
// 执行命令运行nginx
./nginx
//./nginx -c /opt/nginx-1.13.9/conf/nginx.conf 
//  查看是否开启
root@iZ2ze99yw68khbwsm2w698Z:/usr/local/nginx/sbin# ps -ef|grep nginx
root     10699     1  0 14:10 ?        00:00:00 nginx: master process ./nginx -c /opt/nginx-1.13.9/conf/nginx.conf
nobody   10700 10699  0 14:10 ?        00:00:00 nginx: worker process
root     10705  1399  0 14:10 pts/0    00:00:00 grep --color=auto nginx

//  在浏览器输入ip访问出现  Welcome to nginx! 成功~~~

配置nginx.conf


// 打开配置文件
vi /usr/local/nginx/conf/nginx.conf
修改为:
#user  nobody;
worker_processes  1;

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;
	include conf.d/*.conf; # 读取conf.d下的  .conf配置文件
}
// 新建conf.d文件
// 新建一个.conf文件 配置如下

nginx子配置文件

静态资源访问,请求转发

server {
     
    listen        8092;  #监听端口
    server_name 127.0.0.1;#监听的ip
    charset        utf-8;#字符集

   #请求路径  http:127.0.0.1:8092/api/crawle/search
   #转发后的路径 http://192.168.1.55:8086/api/crawler/search
	location /api/crawler {
     
	proxy_pass http://192.168.1.55:8086/api/crawler ;
	}

	#访问路径拼接 upload 访问本地绝对路径下的某图片 
    location /images/ {
     
        alias E:/crawler/images/;
        autoindex on;
    }

	#请求路径  http:127.0.0.1:8092/images/20200115/1.jpg
    #图片路径  E:/crawler/images/20200115/1.jpg
	# 精细化 配置相关静态资源参数,优化访问静态资源文件
    location ~ .*\.(gif|jpg|jpeg|png)$ {
     
        expires 24h;  
        root E:/crawler/;#指定图片存放路径  
        proxy_store on;  
        proxy_temp_path    E:/crawler/;#图片访问路径  
        proxy_redirect     off;  
        proxy_set_header    Host 127.0.0.1;  
        client_max_body_size  10m;
        client_body_buffer_size 1280k;  
        proxy_connect_timeout  900;  
        proxy_send_timeout   900;  
        proxy_read_timeout   900;  
        proxy_buffer_size    40k;  
        proxy_buffers      40 320k;  
        proxy_busy_buffers_size 640k;  
        proxy_temp_file_write_size 640k;  

    }
} 

相关命令

// 检测配置文件
./sbin/nginx -t
root@iZ2ze99yw68khbwsm2w698Z:/usr/local/nginx# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

//  配置文件错误的
root@iZ2ze99yw68khbwsm2w698Z:/usr/local/nginx# ./sbin/nginx -t
nginx: [emerg] unknown directive "erver" in /usr/local/nginx/conf/conf.d/8092.conf:1
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
//  启动   -c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件
./sbin/nginx
//  软启动 
nginx -s reload
//  成功访问到图片

ubuntu Nginx安装和配置,负载均衡_第1张图片

配置域名

# 参考阿里云文档: https://help.aliyun.com/document_detail/98728.html?spm=5176.2020520154.0.0.47f356a7r20bXb

server {
     
    listen        443;
    server_name xxx;
    charset        utf-8;
    access_log   /usr/local/nginx/logs/access.log;
    error_log    /usr/local/nginx/logs/error.log;
    proxy_redirect http:// https://;

	#关于ssl是否添加与nginx版本的文章  https://blog.csdn.net/ootw/article/details/81059677
    #ssl                     off; # “ ssl on ; ” 注释掉或者修改成 “ ssl off ;”,这样,Nginx就可以同时处理HTTP请求和HTTPS请求了。
    ssl_certificate         /usr/local/nginx/ssl/xxxx.pem;
    ssl_certificate_key     /usr/local/nginx/ssl/xxxx.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    # 精细化 配置相关静态资源参数,优化访问静态资源文件
    location ~ .*\.(gif|jpg|jpeg|png)$ {
     
        expires 24h;
        root /home/;#指定图片存放路径
        proxy_store on;
        proxy_temp_path    /home/;#图片访问路径
        proxy_redirect     off;
        proxy_set_header    Host 127.0.0.1;
        client_max_body_size  10m;
        client_body_buffer_size 1280k;
        proxy_connect_timeout  900;
        proxy_send_timeout   900;
        proxy_read_timeout   900;
        proxy_buffer_size    40k;
        proxy_buffers      40 320k;
        proxy_busy_buffers_size 640k;
        proxy_temp_file_write_size 640k;
    }
}

配置开机自启

使用脚本来启动nginx
rc.local脚本是一个ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令。该脚本位于/etc/init.d/路径下。

1.编写脚本

// 打开/etc/init.d/
cd /etc/init.d/
touch nginx.sh
vi nginx.sh
    加入下面的脚本
#!/bin/sh
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.  This example start a
#                    single forking daemon capable of writing a pid
#                    file.  To get other behavoirs, implemend
#                    do_start(), do_stop() or other functions to
#                    override the defaults in /lib/init/init-d-script.

sudo -S /usr/local/nginx/sbin/nginx << EOF
root密码
EOF
exit 0
### END INIT INFO

2.设置权限

//  给nginx.sh可执行权限,这里我就直接给的最高权限 777 
chmod -R 777 nginx.sh

3.加入开机自启

//  执行如下指令添加开机启动,在这里90表明一个优先级,越高表示执行的越晚
update-rc.d nginx.sh defaults 90 

//  移出启动
update-rc.d -f nginx.sh remove

nginx负载均衡配置

upstream test.com{
    server 127.0.0.1:8083 weight=2;  # weight权重有限数值大的
    server 127.0.0.1:8084 weight=1;
}
server {
    listen        8800;
    server_name 183.6.161.170;
    charset        utf-8;
    access_log   D:/tools/nginx-1.16.1/nginx-1.16.1/logs/access.log;
    error_log    D:/tools/nginx-1.16.1/nginx-1.16.1/logs/error.log;
   
	location /service  {
        #set $args  $args+"&custom=td";  
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
        proxy_set_header   Host    $host;
        proxy_set_header   Remote_Addr    $remote_addr;
        proxy_set_header   X-Real-IP    $remote_addr;
        proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_pass http://test.com/; ## 如果设置的有前缀记得这里加上 / ,api里面有service就是http://test.com/service/
        
      	proxy_http_version 1.1;
      	proxy_set_header Upgrade $http_upgrade;
      	proxy_set_header Connection "upgrade";
     } 
}

你可能感兴趣的:(nginx,java)