Nginx服务器配置

目录

一 安装Nginx

  • 1.8安装失败,下的1.14来安装的

1.1 安装步骤

参考文档: https://www.cnblogs.com/dtiove/p/5924385.html
1. 下载源码后解压
2. 进入解压后的目录,输入如下语句

/usr/sbin/nginxsudo ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock

niginx.conf地址为: /etc/nginx/nginx.conf

  1. 安装
make && make install

二 Nginx操作方式

2.1 启动Nginx

  • 进入nginx所在根目录
    windows:
  • 执行nginx.exe或start nginx

linux:
- ./nginx

2.2 停止nginx

  • nginx -s stop 或 nginx -s quit

2.3 查看nginx版本

  • nginx -v 或 nginx -V

2.4 修改配置文件后重载入

  • nginx -s reload(不用手动重启nginx)

2.5 测试当前配置文件是否正确

  • nginx -t

三 Nginx反向代理配置(配置nginx.conf)

3.1 操作步骤

  1. 备份nginx.conf(文件目录查看安装时的设置)
  2. 修改nginx.conf
    • 在http字典下设置upstream,定义该upstream的负载均衡算法以及各主机
    # 在http下设置upstrean字段,在该字段下,设置的server值为该集群下的tomcat主
机的ip地址和端口号
    # 注意:设置了upstream之后需要在location字段再设置添加上http://upstream的
名字,这个upstream的设置才能使用起来
    upstream test_gupan{
        ip_hash; # 需要设置的负载均衡算法,其他的还有轮询等
            server 172.16.225.1:8080;
            server 172.16.225.129:8080;
    }
  • 在http下的server字典下,找到location的设置,将设置的upstream添加至location下
        location / {
            # root   html;
            # index  index.html index.htm;
            # 这里要在proxy_pass配置好设置的userstream,才能实现负载均衡
            proxy_pass http://test_gupan;
        }
  • 设置保持连接时间,一般为65~120ms
        keepalive_timeout  65; # 保持连接的时间,这个值不要设置的太大,设置为65或120(>毫秒级)即可
  1. 启动nginx

启动时可能会报错(2018/06/25 09:43:18 [error] 4694#0: open() “/var/run/nginx/nginx.pid” failed (2: No such file or directory)),意思是/var/run/nginx/nginx.pid找不到,nginx.pid的路径为config文件中的第9行设置的文件路径,解决办法如下:
1. sudo nginx -c nginx配置文件路径(该路径查看安装时的设置)
2. sudo nginx s reload(重新载入配置文件)

  1. nginx负载均衡效果

3.2 nginx.conf配置文件设置案例


#user  nobody;
worker_processes  2; # 查看/proc/cpuinfo里面processor的最多的数目

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 删除pid这一行的注释,不然启动nginx会报错
pid        /var/run/nginx/nginx.pid;


events {
    worker_connections  1024; #设置最大连接数,设置为2时是1024,设置为4时是2048,以此类推
}


http {
    # 设置
        # 在http下设置upstrean字段,在该字段下,设置的server值为该集群下的tomcat主机的ip地址和端口号
        # 注意:设置了upstream之后需要在location字段再设置添加上http://upstream的名字,这个upstream的设置才能使用起来
    upstream test_gupan{
        ip_hash; # 需要设置的负载均衡算法,其他的还有轮询等
            server 172.16.225.1:8080;
            server 172.16.225.129:8080;
    }
    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; # 保持连接的时间,这个值不要设置的太大,设置为65或120(毫秒级)即可

    # gzip这个字段的设置默认是关闭的,但是这个字段设置了可以帮助我们帮图片等资源文件进行压缩,传送到后端服务器,节省带宽.
    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;
            # 这里要在proxy_pass配置好设置的userstream,才能实现负载均衡
            proxy_pass http://test_gupan;
        }


        # 错误页面输出等 
        #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;
        #}
    }


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

}

3.3 设置完成之后负载均衡测试

  • 输入-nginx -s reload重新加载配置文件
  • 在浏览器地址栏输入配置的nginx的IP地址,分别关闭ip地址为172.16.225.1:8080和172.16.225.129:8080的主机,看nginx的跳转
  • 当关闭172.16.225.1主机后,访问nginx的配置的ip地址后,访问的是172.16.225.129:8080的主机
  • 当关闭172.16.225.129主机后,访问nginx的配置的IP地址后,访问的是172.16.225.1:8080的主机

四 Nginx配置文件再多看看

  • nginx反向代理的指令不需要新增额外的模块,默认自带proxv_pass指令,只需要修改配置文件就可以实现反向代理
upstream myserver{
    server 192.168.1.116 down; # down表示当前的server不参与负载
    server 192.168.1.117 backup; # backup表示其他所有的非backup机器比较忙的时候,请求abckup机器,所以这台机器的压力会最轻

    # weight参数表示权值,权值默认为1,权值越高被分配到的几率越大(所占几率是自身权值与所有权值总和的比值)
    server 192.168.1.121 weight=1;
    server 192.168.1.122 weight=2;
}
内容概要:本文档提供了关于“微型车间生产线的设计与生产数据采集试验研究”的毕业设计复现代码,涵盖从论文结构生成、机械结构设计、PLC控制系统设计、生产数据采集与分析系统、有限元分析、进度管理、文献管理和论文排版系统的完整实现。通过Python代码和API调用,详细展示了各个模块的功能实现和相互协作。例如,利用SolidWorks API设计机械结构,通过PLC控制系统模拟生产流程,使用数据分析工具进行生产数据的采集和异常检测,以及利用进度管理系统规划项目时间表。 适合人群:具有机械工程、自动化控制或计算机编程基础的学生或研究人员,尤其是从事智能制造领域相关工作的人员。 使用场景及目标:①帮助学生或研究人员快速搭建和理解微型车间生产线的设计与实现;②提供完整的代码框架,便于修改和扩展以适应不同的应用场景;③作为教学或科研项目的参考资料,用于学习和研究智能制造技术。 阅读建议:此资源不仅包含详细的代码实现,还涉及多个学科领域的知识,如机械设计、电气控制、数据分析等。因此,在学习过程中,建议读者结合实际操作,逐步理解每个模块的功能和原理,并尝试调整参数以观察不同设置下的系统表现。同时,可以参考提供的文献资料,深入研究相关理论和技术背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值