首页 > 其他 > 详细

Nginx配置

时间:2020-02-21 12:46:43      阅读:69      评论:0      收藏:0      [点我收藏+]

执行安装命令

apt-get install nginx

执行命令查看nginx的状态和版本

systemctl status nginx 查看状态

nginx -v 查看版本

nginx服务的几种状态

systemctl stop nginx           //停止服务

systemctl start nginx          //开启服务

systemctl reload nginx         //重新加载配置文件

systemctl restart nginx。       //重新启动服务

systemctl disable nginx           //关闭开机自动启动

systemctl enable nginx                 //开启开机自动启动

配置文件

/etc/nginx,默认的配置文件nginx.conf,一般情况下不去动这个配置文件,凡事需要用到的配置文件都存储在/etc/nginx/conf.d下,默认在nginx.conf中已经配置好会将该文件目录下的所有内容加载, 

所有配置文件在/etc/nginx/conf.d存储时都要以.conf结尾,例如website.conf

默认的default配置文件在/etc/nginx/sites-enabled

默认的根目录在/var/www/html下

Nginx配置主目录和虚拟目录

实例:用户访问http://www.lurenjie.com访问具体网站内容,访问http://www.lurenjie.com/game访问虚拟目录内容,两者是相对独立的。前提可以正常解析域名并访问

在/etc/nginx/conf.d下创建一个default.conf文件(这个名字无所谓,主要是好记,每个网站都会有自己不同的配置文件,避免混淆取一个自己比较容易记住的名字即可),使用vi命令进行编辑,如下:

server{

listen 80;                   

server_name www.lurenjie.com; 

index index.html index.htm;

location / { ?

      root /usr/local/nginx/html;

      }

location /game{ ?      

alias /usr/www/site;

      }

}

如上图,需要注意对应的路径下有对应的index.html方可正确访问对应的页面。注意root和alias的用法

 

Nginx配置负载均衡

需求:192.168.1.188和192.168.1.88两台服务器部署了同样的网站(多台服务器的配置方法类似),保证可以进行高并发,用户访问域名www.lurenjie.com后依次跳转到188和88服务器负载

在/etc/nginx/conf.d下创建一个load.conf文件,输入如下配置,注意weight=1说明权重是1,就是依次进行请求响应。

upstream tomcat_server  { 

        server 192.168.1.188:8080 weight=1;      

        server 192.168.1.88:8080 weight=1; 

    }  

    server { 

        listen        80;  

        server_name  www.lurenjie.com; 

 

        location / {  

            proxy_pass http://tomcat_server/;  

            proxy_redirect default;  

            proxy_set_header Host $http_host;

            proxy_set_header X-Forward-For $remote_addr;

        }

   error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html; 

        }

}

Nginx配置

原文:https://www.cnblogs.com/lurj/p/12340459.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!