点赞
评论
收藏
分享
举报
centOS7安装nginx及nginx配置
发表于2020-09-02 16:11

浏览 10.3k

安装所需插件

1、安装gcc

gcc是linux下的编译器在此不多做解释,感兴趣的小伙伴可以去查一下相关资料,它可以编译 C,C++,Ada,Object C和Java等语言

命令:查看gcc版本 

gcc -v

一般阿里云的centOS7里面是都有的,没有安装的话会提示命令找不到,

安装命令:

yum -y install gcc

2、pcre、pcre-devel安装

pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库。

安装命令:

yum install -y pcre pcre-devel

3、zlib安装

zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装

安装命令:

yum install -y zlib zlib-devel

4、安装openssl

openssl是web安全通信的基石,没有openssl,可以说我们的信息都是在裸奔。。。。。。

安装命令:

yum install -y openssl openssl-devel

安装nginx

1、下载nginx安装包

wget http://nginx.org/download/nginx-1.9.9.tar.gz  

2、把压缩包解压到usr/local/java

tar -zxvf  nginx-1.9.9.tar.gz

3、切换到cd /usr/local/java/nginx-1.9.9/下面

执行三个命令:

  1. ./configure
  2. make
  3. make install

5、切换到/usr/local/nginx安装目录

6、配置nginx的配置文件nginx.conf文件,主要也就是端口

可以按照自己服务器的端口使用情况来进行配置

ESC键,wq!强制保存并退出

7、启动nginx服务

切换目录到/usr/local/nginx/sbin下面

启动nginx命令:

./nginx

8、查看nginx服务是否启动成功

ps -ef | grep nginx

9、访问你的服务器IP

显示

说明安装和配置都没问题OK了

nginx.conf说明

  1. #user nobody;
  2. worker_processes 1; #工作进程:数目。根据硬件调整,通常等于cpu数量或者2倍cpu数量。
  3. #错误日志存放路径
  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7. #pid logs/nginx.pid; # nginx进程pid存放路径
  8. events {
  9. worker_connections 1024; # 工作进程的最大连接数量
  10. }
  11. http {
  12. include mime.types; #指定mime类型,由mime.type来定义
  13. default_type application/octet-stream;
  14. # 日志格式设置
  15. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. # '$status $body_bytes_sent "$http_referer" '
  17. # '"$http_user_agent" "$http_x_forwarded_for"';
  18. #access_log logs/access.log main; #用log_format指令设置日志格式后,需要用access_log来指定日志文件存放路径
  19. sendfile on; #指定nginx是否调用sendfile函数来输出文件,对于普通应用,必须设置on。
  20. 如果用来进行下载等应用磁盘io重负载应用,可设着off,以平衡磁盘与网络io处理速度,降低系统uptime。
  21. #tcp_nopush on; #此选项允许或禁止使用socket的TCP_CORK的选项,此选项仅在sendfile的时候使用
  22. #keepalive_timeout 0; #keepalive超时时间
  23. keepalive_timeout 65;
  24. #gzip on; #开启gzip压缩服务
  25. #虚拟主机
  26. server {
  27. listen 80; #配置监听端口号
  28. server_name localhost; #配置访问域名,域名可以有多个,用空格隔开
  29. #charset koi8-r; #字符集设置
  30. #access_log logs/host.access.log main;
  31. location / {
  32. root html;
  33. index index.html index.htm;
  34. }
  35. #错误跳转页
  36. #error_page 404 /404.html;
  37. # redirect server error pages to the static page /50x.html
  38. #
  39. error_page 500 502 503 504 /50x.html;
  40. location = /50x.html {
  41. root html;
  42. }
  43. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  44. #
  45. #location ~ \.php$ {
  46. # proxy_pass http://127.0.0.1;
  47. #}
  48. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  49. #
  50. #location ~ \.php$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
  51. # root html; #根目录
  52. # fastcgi_pass 127.0.0.1:9000; #请求转向定义的服务器列表
  53. # fastcgi_index index.php; # 如果请求的Fastcgi_index URI是以 / 结束的, 该指令设置的文件会被附加到URI的后面并保存在变量$fastcig_script_name中
  54. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  55. # include fastcgi_params;
  56. #}
  57. # deny access to .htaccess files, if Apache's document root
  58. # concurs with nginx's one
  59. #
  60. #location ~ /\.ht {
  61. # deny all;
  62. #}
  63. }
  64. # another virtual host using mix of IP-, name-, and port-based configuration
  65. #
  66. #server {
  67. # listen 8000;
  68. # listen somename:8080;
  69. # server_name somename alias another.alias;
  70. # location / {
  71. # root html;
  72. # index index.html index.htm;
  73. # }
  74. #}
  75. # HTTPS server
  76. #
  77. #server {
  78. # listen 443 ssl; #监听端口
  79. # server_name localhost; #域名
  80. # ssl_certificate cert.pem; #证书位置
  81. # ssl_certificate_key cert.key; #私钥位置
  82. # ssl_session_cache shared:SSL:1m;
  83. # ssl_session_timeout 5m;
  84. # ssl_ciphers HIGH:!aNULL:!MD5; #密码加密方式
  85. # ssl_prefer_server_ciphers on; # ssl_prefer_server_ciphers on; #
  86. # location / {
  87. # root html;
  88. # index index.html index.htm;
  89. # }
  90. #}
  91. }
已修改于2023-03-09 02:03
创作不易,留下一份鼓励
张东明001

暂无个人介绍

关注



写下您的评论
发表评论
全部评论(0)

按点赞数排序

按时间排序

关于作者
张东明001
这家伙很懒还未留下介绍~
19
文章
1
问答
16
粉丝
相关文章
2018-02-20刚刚更新的nginx-1.13.9已支持HTTP/2的特性ServerPush,这个特性目的是让服务端将部分资源主动推送给浏览器,节约浏览器需要使用这些资源时再次发送Get请求的时间。很长的一段时间内Nginx都是不支持这个特性的,不过在新版本中已经可以使用,详细改动可以查看 http://hg.nginx.org/nginx/rev/641306096f5bNginx中的ServerPush提供两个选项:直接指定需要推送的资源,Nginx会直接推送(只能使用相对链接)。http2_push/css/style.css; 使用 W3C文档 规定的 Link 字段作为HTML响应。Link:;as=style;rel=preload; 然后在配置中开启http2_push_preloadon; 为了方便,我选择第一种方式,直接指定需要推送的资源文件。之后在Chrome的DeveloperTools——Network面板中可以看到我指定推送的资源状态为
点赞 2
浏览 3.7k
当上游出错时,作为负载均衡的Nginx可以实时更换Server,在客户端无感知的情况下重新转发HTTP请求。这一功能在Nginx指令中称为nextupstream,本文将详细介绍其用法及实现原理。在OSI网络模型中,传输层的TCP协议通过内核提供的系统调用向Nginx反馈错误,表示层的TLS/SSL协议通过openssl库向Nginx返回错误,而应用层的HTTP协议(或者uwsgi、gRPC、CGI、memcached等协议)通过Response的Decode解码流程返回错误。当Nginx能够通过重试解决这些错误时,我们可以使用nextupstream机制对客户端隐藏个别上游Server由于宕机、网络异常产生的错误,这可以极大的提升整个分布式系统的可用性。如果我们不清楚它处理协议错误及重试转发的原理,就很容易在实际场景中发现next upstream没有发挥作用,比如:l
点赞 2
浏览 6k
该版本解决了 PHP 和 Python 应用处理、NJS 集成和 WebSocket 中的各种错误 了解更多
点赞 1
浏览 6.2k