Nginx常用配置与命令

标签: 

正在查看 2 条回复
  • 作者
    帖子
    • okass - WirelessLink社区okass
      参与者
      #1033
      Up
      0
      ::

      一、nginx安装
      1、本地安装
      windows系统:
      直接去官网:https://nginx.org/en/download… 下载相应版本即可。

      mac系统:
      $ brew install nginx

      2、Linux安装:
      以centOS系统为例,有下面两种安装方式(推荐1)
      1.) 通过rpm镜像源安装

      $ rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
      $ yum install -y nginx
      2.) 通过依赖包详细安装

      安装nginx依赖库pcre、zlib
      $ yum install pcre pcre-devel
      $ yum install zlib zlib-devel
      如有必要,可以安装c++编译环境和openssl
      $ yum install gcc-c++
      $ yum install openssl openssl-devel
      下载/编译nginx

      $ wget -c https://nginx.org/download/nginx-1.16.0.tar.gz
      $ tar -zxvf nginx-1.16.0.tar.gz

      # 编译安装
      $ cd nginx-1.16.0
      $ ./configure # 默认安装在/usr/local/nginx
      $ make && make install

      # 创建软链
      $ ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
      $ nginx -v

       

      二、nginx命令

      # windows启动
      > start nginx

      # linux/mac启动
      $ service nginx start

      # 手动指定配置
      $ nginx -c /usr/local/nginx/conf/nginx.conf

      # -p指定nginx运行目录(日志存储位置)
      $ nginx -c /path/nginx.conf -p /path/

      # 重启
      $ nginx -s reload

      # 关闭
      $ nginx -s stop

      # 查看端口
      $ netstat -an | grep 端口 # linux/mac系统
      > netstat -an | findstr 端口 # windows系统

      # 测试web服务
      $ curl -i 主机:端口
      # 或
      $ telnet 主机 端口

      # 查看进程
      $ ps -ef | grep nginx

      # 查看错误日志
      $ tail -30 /var/log/nginx/error.log

       

      三、nginx配置
      查看nginx.conf配置文件位置
      $ nginx -t

       

      1、创建一个标准的server
      确保nginx.conf里的 include conf.d/*.conf 已启用,没有则添加一条
      在conf.d目录下新建server.conf文件,配置如下:
      server {
      listen 80;
      server_name 127.0.0.1;

      client_max_body_size 100m;

      location / {
      root /app/xxx; # 项目所在目录
      index index.html index.htm;
      try_files $uri $uri/ /index.html; # vue单页应用需要路由始终指向index.html
      }
      }

       

      2、配置ssl证书实现https访问

      复制.pem和.key两种证书到当前server配置同一个目录下
      server {
      listen 443;
      server_name 127.0.0.1;
      ssl on;
      ssl_certificate my.pem; # 替换成自己的证书
      ssl_certificate_key my.key; # 替换成自己的证书
      ssl_session_timeout 5m;
      ssl_protocols SSLv3 TLSv1.2;
      ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
      ssl_prefer_server_ciphers on;

      location / {}
      }

      3、api接口反向代理
      location /api {
      proxy_pass http://b.domain.com:9000; # 最终地址会加上/api,变成 /api/xxx
      #proxy_cookie_domain b.domain.com a.domain.com; # 需要修改接口返回的cookie域名时使用
      }
      需要注意的是,proxy_pass路径有相对和绝对之分,如:proxy_pass http://b.domain.com:9000/; # 最终地址会替掉/api,变成 /xxx

      4、upstream负载均衡
      upstream apiServer {
      server 10.0.0.80:5000; # 如果需要权重加 weight=数字
      server 10.0.0.81:5000;
      }
      server {
      listen 80;
      server_name 127.0.0.1;

      location /api {
      proxy_pass http://apiServer;
      }
      }
      需要注意的是:

      upstream名称不应包含下划线,因为在某些条件下,当成主机名传给后端Java应用,会被当做域名来解析,结果返回Null,容易触发服务器内部错误。建议:使用驼峰命名规范

      5、允许它站跨域访问
      在location /api {}里添加以下项:

      add_header Access-Control-Allow-Origin *; # *表示允许所有站跨域访问(不安全,建议指定具体允许的域名如:http://b.domain.com:9000(注意格式:http(s):// + domain + port,末尾也不能加/)
      add_header Access-Control-Allow-Credentials true; #此项为允许带cookie跨域访问,若设置true,上面域名配置不能为*,必须指定具体域名

      6、iframe同源策略限制
      可以通过nginx配置,限制它站通过iframe嵌入访问本站,不加配置默认是允许访问
      # 限制为同源可用
      add_header X-Frame-Options SAMEORIGIN

      # 指定网站可用
      add_header X-Frame-Options “ALLOW-FROM http://xxx.com:8000”;
      add_header Content-Security-Policy “frame-ancestors http://xxx.com:8000”; # 兼容chrome

      7、开启gzip压缩
      gzip字段设置on,并设置哪些类型文件需要压缩:

      http {
      include mime.conf;
      default_type application/octet-stream;
      # ….

      gzip on;
      gzip_min_length 10k;
      gzip_comp_level 5;
      gzip_types text/plain text/css application/x-javascript application/javascript text/javascript;

      server {
      # ….
      }

      8、其它问题
      1.) 访问服务报403权限

      需要修改nginx.conf里的user,比如user root;
      2.) nginx重启时报pid错

      nginx: [error] invalid PID number “” in “/usr/local/nginx/logs/nginx.pid”
      解决方案:使用nginx -c的参数指定nginx.conf文件的位置

      # 新建nginx.pid
      $ mkdir -p /usr/local/nginx/logs
      $ touch /usr/local/nginx/logs/nginx.pid

      # 指定配置文件

      $ nginx -c /usr/local/nginx/conf/nginx.conf

       

      访问ChatGPT的可用VPS机房IP推荐 Lisahost美国原生IP搬瓦工美西DMITTripodcloudFrantech
    • okass - WirelessLink社区okass
      参与者
      #1034
      Up
      0
      ::

      Nginx调整上传文件尺寸大小

      client_max_body_size在 Nginx 配置文件中 找到设置/etc/nginx/conf-enabled/client_max_body_size.conf,将大小更改为合适的值以满足您的需要。

      #sudo vim /etc/nginx/conf-enabled/client_max_body_size.conf

      client_max_body_size 200m;

       

      需要重新加载或重启 Nginx 服务。

      # sudo service nginx restart

      访问ChatGPT的可用VPS机房IP推荐 Lisahost美国原生IP搬瓦工美西DMITTripodcloudFrantech
    • okass - WirelessLink社区okass
      参与者
      #1035
      Up
      0
      ::

      Nginx 解决504 gate-way timesout 超时错误的问题

       

      1> 可以调整nginx 内存相关参数

      # sudo vim /etc/nginx/sites-enabled/00-default-ssl.conf

      在server{ }段中加入如下代码

      #fix 504 gateway错误

      location ~ .php$ {

      #include snippets/fastcgi-php.conf;

      #fastcgi_pass 127.0.0.1:9000;

      fastcgi_read_timeout 300;

      fastcgi_send_timeout 300;

      #proxy_send_timeout 300; #如果使用了代理则加上proxy参数

      #proxy_read_timeout 300;

      }

      #end

      重启nginx

      # sudo service nginx reload

       

      2> 调整php参数 request_terminate_timeout , max_execution_time

      #sudo vim /etc/php/7.4/fpm/pool.d/www.conf

      request_terminate_timeout = 300

      #sudo vim /etc/php/7.4/fpm/php.ini

      max_execution_time = 300

       

      3> 重启nginx 和php-fpm

      # sudo service nginx reload

      # sudo service php7.4-fpm reload

       

      我的中国心

      我的中国心

      我ode中国心

      访问ChatGPT的可用VPS机房IP推荐 Lisahost美国原生IP搬瓦工美西DMITTripodcloudFrantech
正在查看 2 条回复
  • 哎呀,回复话题必需登录。
WirelessLink社区
Logo