nginx在centos服务器上的操作

nginx相关配置操作

1 下载和安装

下载连接

https://nginx.org/en/download.html

安装和配置教程

https://www.cnblogs.com/fengff/p/8892590.html

2 使用

server模块

srever模块配置是http模块中的一个子模块,用来定义一个虚拟访问主机,也就是一个虚拟服务器的配置信息

server {
    listen        80;
    server_name localhost    192.168.1.100;
    root        /nginx/www;
    index        index.php index.html index.html;
    charset        utf-8;
    access_log    logs/access.log;
    error_log    logs/error.log;
    ......
}

核心配置信息如下:

  • server:一个虚拟主机的配置,一个http中可以配置多个server
  • server_name:用力啊指定ip地址或者域名,多个配置之间用空格分隔
  • root:表示整个server虚拟主机内的根目录,所有当前主机中web项目的根目录
  • index:用户访问web网站时的全局首页
  • charset:用于设置www/路径中配置的网页的默认编码格式
  • access_log:用于指定该虚拟主机服务器中的访问记录日志存放路径
  • error_log:用于指定该虚拟主机服务器中访问错误日志的存放路径

location模块

location模块是nginx配置中出现最多的一个配置,主要用于配置路由访问信息

在路由访问信息配置中关联到反向代理、负载均衡等等各项功能,所以location模块也是一个非常重要的配置模块

基本配置

location / {
    root    /nginx/www;
    index    index.php index.html index.htm;
}

location /:表示匹配访问根目录

root:用于指定访问根目录时,访问虚拟主机的web目录

index:在不指定访问具体资源时,默认展示的资源文件列表

反向代理配置方式

通过反向代理代理服务器访问模式,通过proxy_set配置让客户端访问透明化

location / {
    proxy_pass http://localhost:8888;
    proxy_set_header X-real-ip $remote_addr;
    proxy_set_header Host $http_host;
}

uwsgi配置

wsgi模式下的服务器配置访问方式

location / {
    include uwsgi_params;
    uwsgi_pass localhost:8888
}

upstream模块

upstream模块主要负责负载均衡的配置,通过默认的轮询调度方式来分发请求到后端服务器

简单的配置方式如下

upstream name {
    ip_hash;
    server 192.168.1.100:8000;
    server 192.168.1.100:8001 down;
    server 192.168.1.100:8002 max_fails=3;
    server 192.168.1.100:8003 fail_timeout=20s;
    server 192.168.1.100:8004 max_fails=3 fail_timeout=20s;
}

核心配置信息如下

  • ip_hash:指定请求调度算法,默认是weight权重轮询调度,可以指定
  • server host:port:分发服务器的列表配置
  • -- down:表示该主机暂停服务
  • -- max_fails:表示失败最大次数,超过失败最大次数暂停服务
  • -- fail_timeout:表示如果请求受理失败,暂停指定的时间之后重新发起请求

3 增加站点操作

  1. 修改nginx/conf下的 nginx.conf

    增加网点配置

 server {
        listen       端口; #必须是服务器开放的
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /网站文件路径/dist/;
            index  index.html index.htm index login;
            try_files $uri $uri/ /index.html;	
        }

      #代理XXx后台接口
         location /别名/ {
             proxy_set_header Host $http_host;
		     proxy_set_header X-Real-IP $remote_addr;
		     proxy_set_header REMOTE-HOST $remote_addr;
		     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		     proxy_pass http://192.168.xx.12:xxx/;
       }

  
        #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;
        }
    }
  1. 查看服务器是否端口

(1)查看对外开放的端口状态 查询已开放的端口 netstat -anp

查询指定端口是否已开 firewall-cmd --query-port=666/tcp 提示 yes,表示开启;no表示未开启。

(2)查看防火墙状态
查看防火墙状态 systemctl status firewalld
开启防火墙 systemctl start firewalld
关闭防火墙 systemctl stop firewalld
开启防火墙 service firewalld start
若遇到无法开启
先用:systemctl unmask firewalld.service
然后:systemctl start firewalld.service

(3)对外开发端口 查看想开的端口是否已开: firewall-cmd --query-port=6379/tcp

(4) 添加指定需要开放的端口: firewall-cmd --add-port=123/tcp --permanent

​ 重载入添加的端口: firewall-cmd --reload 查询指定端口是否开启成功: firewall-cmd --query-port=123/tcp

相关 链接

https://blog.csdn.net/realjh/article/details/82048492

如果出现错误 FirewallD is not running

参考链接

https://jingyan.baidu.com/article/5552ef47f509bd518ffbc933.html

  1. 重启nginx 操作

参考

停止和检查参考

https://www.cnblogs.com/codingcloud/p/5095066.html

1) 查看进程

 ps -ef|grep nginx

2)杀死进程

 kill -QUIT 2072

3)j检查文件是否配置正确

进入nginx安装目录sbin下,输入命令./nginx -t

看到如下显示nginx.conf syntax is ok

nginx.conf test is successful

说明配置文件正确

启动参考

进入nginx安装目录sbin下

在命令里输入

/tools/nginx/sbin/nginx   -c /tools/nginx/conf/nginx.conf

这里/tools/nginx/sbin服务器目录,

大家记得灵活变动。

https://jingyan.baidu.com/article/60ccbceb9fbeff64cab197b6.html

可以访问网站了。

原文地址:https://www.cnblogs.com/xiaohuasan/p/15783426.html