服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - Linux环境下安装Nginx及其使用

Linux环境下安装Nginx及其使用

2022-03-02 17:12明天你好向前奔跑 Linux

这篇文章主要介绍了Linux环境下安装Nginx及其使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、查看centos的版本

?
1
cat /etc/redhat-release

二、添加资源库

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

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

三、安装nginx

?
1
yum -y install nginx

Linux环境下安装Nginx及其使用

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

?
1
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实现停止、启动、重启

?
1
2
3
4
systemctl stop nginx.service 
systemctl start nginx.service
systemctl restart nginx.service
systemctl status nginx.service

开机自启:

?
1
systemctl enable nginx.service

取消开机自启:

?
1
systemctl disable nginx.service

六、nginx.conf配置示例

我的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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 指令开始
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/860a4dec6be3

延伸 · 阅读

精彩推荐
  • Linux详解在Linux系统中如何管理systemd服务

    详解在Linux系统中如何管理systemd服务

    这篇文章主要介绍了详解在Linux系统中如何管理systemd服务,文中展示相关很多常用命令,需要的朋友可以参考下 ...

    服务器之家3292019-09-16
  • Linux详解Linux查看程序端口占用情况

    详解Linux查看程序端口占用情况

    本篇文章主要介绍了详解Linux查看程序端口占用情况 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    郭振斌4322022-01-12
  • Linuxlinux修改系统时间的详细方法介绍

    linux修改系统时间的详细方法介绍

    WINDOWS下修改时间很简单.今天介绍下如何在linux下修改时间 ...

    Linux教程网3832019-12-19
  • LinuxLinux中利用sudo进行赋权的方法详解

    Linux中利用sudo进行赋权的方法详解

    sudo的目的:为非根用户授予根用户的权限,下面这篇文章主要给大家介绍了关于Linux中利用sudo进行赋权的相关资料,文中通过示例代码介绍的非常详细,需...

    Linux中国11262022-02-25
  • LinuxLinux命令下crontab命令

    Linux命令下crontab命令

    crontab是一个用来设置、删除或显示供守护进程cron执行的定时任务的命令。下面给大家介绍linux命令下crontab命令,需要的朋友一起看看吧...

    Chentf9642021-11-25
  • LinuxLinux iptables基础知识和规则原理讲解

    Linux iptables基础知识和规则原理讲解

    这篇文章主要介绍了Linux iptables基础知识和规则原理讲解,本文讲解了什么是iptables、什么是Netfilter、iptables规则原理等内容,需要的朋友可以参考下 ...

    服务器之家4542019-09-28
  • Linuxlinux tomcat配置https的方法

    linux tomcat配置https的方法

    这篇文章主要介绍了linux tomcat配置https的方法,需要的朋友可以参考下...

    互联网编程6362022-02-13
  • LinuxLinux系统中swap的真正作用

    Linux系统中swap的真正作用

    SWAP 的话,OVZ的机器不建议开启 Because openvz机器会优先使用 swap 1、SWAP不是运存,SWAP的大小跟运存的大小没有任何关系。 2、SWAP就是一个临时仓库,只是一...

    九天博客5592020-05-23