搜索
首页运维NginxLinux环境下怎么安装及使用Nginx

Linux环境下怎么安装及使用Nginx

May 12, 2023 pm 02:37 PM
linuxnginx

一、查看centos的版本

cat /etc/redhat-release

二、添加资源库

在 centos 系统上安装 nginx ,得先去添加一个资源库:

vim /etc/yum.repos.d/nginx.repo
  [nginx]
  name=nginx repo
  baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  gpgcheck=0
  enabled=1

三、安装nginx

yum -y install nginx

Linux环境下怎么安装及使用Nginx

四、测试nginx配置文件是否正确

nginx -t

打印如下:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

五、centos7.0+ nginx实现停止、启动、重启

systemctl stop nginx.service  
systemctl start nginx.service
systemctl restart nginx.service
systemctl status nginx.service

开机自启:

systemctl enable nginx.service

取消开机自启:

systemctl disable nginx.service

六、nginx.conf配置示例

我的配置:

user nginx;
worker_processes 4;

error_log /var/log/nginx/error.log warn;
pid    /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include    /etc/nginx/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 /var/log/nginx/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  keepalive_timeout 65;

  #gzip on;

  include /etc/nginx/conf.d/*.conf;

  #设定负载均衡的服务器列表
  upstream pay_server {
    ip_hash;
    server 10.74.248.01:8088 max_fails=2 fail_timeout=2;
    server 10.74.248.02:8088 max_fails=2 fail_timeout=2;
  }

  upstream print_server {
    ip_hash;
    server 10.74.248.03:2001 max_fails=2 fail_timeout=2;
    server 10.74.248.04:2001 max_fails=2 fail_timeout=2;
  }

  upstream accss_door_server {
    ip_hash;
    server 10.74.248.05:2002 max_fails=2 fail_timeout=2;
    server 10.74.248.06:2002 max_fails=2 fail_timeout=2;
  }
  
  server {
    listen 80;
    server_name wab.kupu.ccoop.com.cn;
    location /invengo {
         proxy_pass http://pay_server;
         index index.jsp index.html index.htm;
    }
    location /invengo/epc {
         proxy_pass http://print_server;
         index index.jsp index.html index.htm;
    }
    location /invengo/print {
         proxy_pass http://print_server;
         index index.jsp index.html index.htm;
    }

    location /checkout {
         proxy_pass http://accss_door_server;
         index index.jsp index.html index.htm;
    }
    location /asset {
         proxy_pass http://accss_door_server;
         index index.jsp index.html index.htm;
    }
  }

常用的nginx.conf配置:

user www www;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid    logs/nginx.pid;
events {
  use epoll;
  worker_connections 2048;
}
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 65;
 # gzip压缩功能设置
  gzip on;
  gzip_min_length 1k;
  gzip_buffers  4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 6;
  gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
  gzip_vary on;
 
 # http_proxy 设置
  client_max_body_size  10m;
  client_body_buffer_size  128k;
  proxy_connect_timeout  75;
  proxy_send_timeout  75;
  proxy_read_timeout  75;
  proxy_buffer_size  4k;
  proxy_buffers  4 32k;
  proxy_busy_buffers_size  64k;
  proxy_temp_file_write_size 64k;
  proxy_temp_path  /usr/local/nginx/proxy_temp 1 2;
 # 设定负载均衡后台服务器列表 
  upstream backend { 
       #ip_hash; 
       server  192.168.10.100:8080 max_fails=2 fail_timeout=30s ; 
       server  192.168.10.101:8080 max_fails=2 fail_timeout=30s ; 
  }
 # 很重要的虚拟主机配置
  server {
    listen    80;
    server_name itoatest.example.com;
    root  /apps/oaapp;
    charset utf-8;
    access_log logs/host.access.log main;
    #对 / 所有做负载均衡+反向代理
    location / {
      root  /apps/oaapp;
      index index.jsp index.html index.htm;
      proxy_pass    http://backend; 
      proxy_redirect off;
      # 后端的web服务器可以通过x-forwarded-for获取用户真实ip
      proxy_set_header host $host;
      proxy_set_header x-real-ip $remote_addr; 
      proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
      proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
      
    }
    #静态文件,nginx自己处理,不去backend请求tomcat
    location ~* /download/ { 
      root /apps/oa/fs; 
      
    }
    location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$  
    {  
      root /apps/oaapp;  
      expires   7d; 
    }
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 192.168.10.0/24;
      deny all;
    }
    location ~ ^/(web-inf)/ {  
      deny all;  
    }
    #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;
    }
  }
 ## 其它虚拟主机,server 指令开始
}

以上是Linux环境下怎么安装及使用Nginx的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
NGINX与Apache:比较Web服务器技术NGINX与Apache:比较Web服务器技术May 02, 2025 am 12:08 AM

NGINX适合处理高并发和静态内容,Apache适用于动态内容和复杂URL重写。1.NGINX采用事件驱动模型,适合高并发。2.Apache使用进程或线程模型,适用于动态内容。3.NGINX配置简单,Apache配置复杂但更灵活。

nginx和apache:部署和配置nginx和apache:部署和配置May 01, 2025 am 12:08 AM

NGINX和Apache各有优势,选择取决于具体需求。1.NGINX适合高并发,部署简单,配置示例包括虚拟主机和反向代理。2.Apache适用于复杂配置,部署同样简单,配置示例包括虚拟主机和URL重写。

NGINX单元的目的:运行Web应用程序NGINX单元的目的:运行Web应用程序Apr 30, 2025 am 12:06 AM

NGINXUnit的目的是简化Web应用程序的部署和管理。其优势包括:1)支持多种编程语言,如Python、PHP、Go、Java和Node.js;2)提供动态配置和自动重载功能;3)通过统一的API管理应用生命周期;4)采用异步I/O模型,支持高并发和负载均衡。

NGINX:高性能Web服务器的简介NGINX:高性能Web服务器的简介Apr 29, 2025 am 12:02 AM

NGINX始于2002年,由IgorSysoev开发,旨在解决C10k问题。1.NGINX是高性能Web服务器,基于事件驱动的异步架构,适用于高并发。2.提供反向代理、负载均衡和缓存等高级功能,提升系统性能和可靠性。3.优化技巧包括调整worker进程数、启用Gzip压缩、使用HTTP/2和安全配置。

Nginx vs. Apache:看他们的架构Nginx vs. Apache:看他们的架构Apr 28, 2025 am 12:13 AM

NGINX和Apache在架构上的主要区别在于:NGINX采用事件驱动、异步非阻塞模型,而Apache使用进程或线程模型。1)NGINX通过事件循环和I/O多路复用机制高效处理高并发连接,适合静态内容和反向代理。2)Apache采用多进程或多线程模型,稳定性高但资源消耗大,适合需要丰富模块扩展的场景。

NGINX与Apache:检查优点和缺点NGINX与Apache:检查优点和缺点Apr 27, 2025 am 12:05 AM

NGINX适合处理高并发和静态内容,Apache则适用于复杂配置和动态内容。1.NGINX高效处理并发连接,适合高流量场景,但处理动态内容需额外配置。2.Apache提供丰富模块和灵活配置,适合复杂需求,但高并发性能较差。

nginx和apache:了解关键差异nginx和apache:了解关键差异Apr 26, 2025 am 12:01 AM

NGINX和Apache各有优劣,选择应基于具体需求。1.NGINX适合高并发场景,因其异步非阻塞架构。2.Apache适用于需要复杂配置的低并发场景,因其模块化设计。

NGINX单元:关键功能NGINX单元:关键功能Apr 25, 2025 am 12:17 AM

NGINXUnit是一个开源应用服务器,支持多种编程语言,提供动态配置、零停机更新和内置负载均衡等功能。1.动态配置:无需重启即可修改配置。2.多语言支持:兼容Python、Go、Java、PHP等。3.零停机更新:支持不中断服务的应用更新。4.内置负载均衡:可将请求分发到多个应用实例。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器