如何在CentOS 7上安装和配置Nginx

前端之家收集整理的这篇文章主要介绍了如何在CentOS 7上安装和配置Nginx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.安装CentOS 7 EPEL仓库

  1. sudo yum install epel-release

2.安装Nginx

现在Nginx存储库已经安装在您的服务器上,使用以下yum命令安装Nginx

  1. sudo yum install Nginx

在对提示回答yes后,Nginx将在服务器上完成安装。

3.启动Nginx

Nginx不会自行启动。要运行Nginx,请输入:

  1. sudo systemctl start Nginx

如果您正在运行防火墙,请运行以下命令以允许HTTP和HTTPS通信:

  1. sudo firewall-cmd --permanent --zone=public --add-service=http
  2. sudo firewall-cmd --permanent --zone=public --add-service=https
  3. sudo firewall-cmd --reload

打开浏览器输入ip地址看到Nginx首页就说明你启动成功了

4.设置开机启动

  1. sudo systemctl enable Nginx

5.配置Nginx

使用yum进行安装的Nginx配置文件/etc/Nginx/Nginx.conf

  1. vim /etc/Nginx/Nginx.conf

Nginx.conf:

  1. # For more information on configuration,see:
  2. # * Official English Documentation: http://Nginx.org/en/docs/
  3. # * Official Russian Documentation: http://Nginx.org/ru/docs/
  4.  
  5. user Nginx;
  6.  
  7. #Nginx进程数,建议设置为等于cpu总核心数。
  8. worker_processes auto;
  9.  
  10. #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
  11. error_log /var/log/Nginx/error.log;
  12. pid /run/Nginx.pid;#进程pid文件
  13.  
  14. # Load dynamic modules. See /usr/share/Nginx/README.dynamic.
  15. include /usr/share/Nginx/modules/*.conf;
  16.  
  17. events {
  18. #单个进程最大连接数(最大连接数=连接数*进程数)
  19. #根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台Nginx服务器的最大连接数为。
  20. worker_connections 1024;
  21. }
  22.  
  23. #设定http服务器,利用它的反向代理功能提供负载均衡支持
  24. http {
  25. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  26. '$status $body_bytes_sent "$http_referer" '
  27. '"$http_user_agent" "$http_x_forwarded_for"';
  28.  
  29. access_log /var/log/Nginx/access.log main;
  30.  
  31. #开启高效文件传输模式,sendfile指令指定Nginx是否调用sendfile函数输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
  32. #sendfile指令指定 Nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
  33. sendfile on;
  34.  
  35. #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
  36. tcp_nopush on;
  37. tcp_nodelay on;
  38.  
  39. #长连接超时时间,单位是秒
  40. keepalive_timeout 65;
  41. types_hash_max_size 2048;
  42.  
  43. include /etc/Nginx/mime.types;
  44. default_type application/octet-stream;
  45.  
  46. # Load modular configuration files from the /etc/Nginx/conf.d directory.
  47. # See http://Nginx.org/en/docs/ngx_core_module.html#include
  48. # for more information.
  49. include /etc/Nginx/conf.d/*.conf;
  50.  
  51. #虚拟主机的配置
  52. server {
  53. #监听端口
  54. listen 80 default_server;
  55. listen [::]:80 default_server;
  56.  
  57. #域名可以有多个,用空格隔开
  58. server_name luischen.cn;
  59. # root /usr/share/Nginx/html;
  60. # 重定向至https(按照需求)
  61. rewrite ^(.*)$ https://$host$1 permanent;
  62. # Load configuration files for the default server block.
  63. include /etc/Nginx/default.d/*.conf;
  64.  
  65. location / {
  66. }
  67.  
  68. error_page 404 /404.html;
  69. location = /40x.html {
  70. }
  71.  
  72. error_page 500 502 503 504 /50x.html;
  73. location = /50x.html {
  74. }
  75. }
  76.  
  77. # Settings for a TLS enabled server.
  78.  
  79. server {
  80. # 监听433端口
  81. listen 443 ssl http2 default_server;
  82. listen [::]:443 ssl http2 default_server;
  83. server_name luischen.cn;
  84. #root /usr/share/Nginx/html;
  85. # ssl证书
  86. ssl_certificate "/etc/Nginx/1_luischen.cn_bundle.crt";
  87. ssl_certificate_key "/etc/Nginx/2_luischen.cn.key";
  88. ssl_session_cache shared:SSL:1m;
  89. ssl_session_timeout 10m;
  90. ssl_ciphers HIGH:!aNULL:!MD5;
  91. ssl_prefer_server_ciphers on;
  92.  
  93. #以下是一些反向代理的配置,可选。
  94. proxy_set_header X-Forwarded-Host $host;
  95. proxy_set_header X-Forwarded-Server $host;
  96. #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
  97. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  98. proxy_set_header Host $host;
  99. # # Load configuration files for the default server block.
  100. # include /etc/Nginx/default.d/*.conf;
  101. #
  102. location / {
  103. # 需要代理的端口-也就是Nginx指向本地的端口
  104. proxy_pass http://127.0.0.1:8091;
  105. # 超时时间
  106. proxy_connect_timeout 600;
  107. proxy_read_timeout 600;
  108. }
  109.  
  110. error_page 404 /404.html;
  111. location = /40x.html {
  112. }
  113.  
  114. error_page 500 502 503 504 /50x.html;
  115. location = /50x.html {
  116. }
  117. }
  118. }

6.Nginx启动和停止命令

启动

  1. sudo systemctl start Nginx

停止

  1. sudo systemctl stop Nginx

猜你在找的CentOS相关文章