gcc

1、查看gcc版本
gcc -v
2、gcc安装命令
yum -y install gcc

pcre和pcre-devel

nginx的http模块使用pcre来解析正则表达式
yum install -y pcre pcre-devel

zlib

nginx使用zlib对http包的内容进行gzip。
yum install -y zlib zlib-devel

openssl

openssl用于数据链路通信安全加密。
yum install -y openssl openssl-devel

安装nginx

1、去官网获取最新稳定版本下载链接,官网下载页面地址:http://nginx.org/en/download.html
image.png

2、在linux上,利用wget命令下载nginx
wget http://nginx.org/download/nginx-1.20.2.tar.gz

3、解压到你要存放的目标,我这里是放在/application。解压完毕,会看到对应的目录里面多出一个nginx-1.20.2的文件夹
tar -zxvf nginx-1.20.2.tar.gz -C /application[

4、切换到对应的解压目录,对nginx进行编译安装。按以下步骤执行命令](https://blog.csdn.net/lishuaipiao/article/details/116453998)

  1. # 不需要https模块的, 这里只输入./configure即可
  2. ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  3. # 编译
  4. make
  5. # 安装
  6. make install

5、启动nginx。当make install命令执行完,我们会看到/usr/local会多出一个nginx文件夹。我们切换到/usr/local/nginx/sbin,进行启动nginx。如果需要修改端口等其他配置信息,进入/usr/local/nginx/conf修改nginx.conf的里面的信息。

  1. # 启动
  2. ./nginx -s start
  3. # 刷新配置
  4. ./nginx -s reload
  5. # 停止nginx
  6. ./nginx -s stop
  7. # 查看nginx是否启动成功
  8. ps -ef | grep nginx

配置nginx开机自启

1、在/etc/init.d下创建文件nginx,具体可参考官网的
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

  1. #!/bin/sh
  2. #
  3. # nginx - this script starts and stops the nginx daemon
  4. #
  5. # chkconfig: - 85 15
  6. # description: NGINX is an HTTP(S) server, HTTP(S) reverse \
  7. # proxy and IMAP/POP3 proxy server
  8. # processname: nginx
  9. # config: /etc/nginx/nginx.conf
  10. # config: /etc/sysconfig/nginx
  11. # pidfile: /var/run/nginx.pid
  12. # Source function library.
  13. . /etc/rc.d/init.d/functions
  14. # Source networking configuration.
  15. . /etc/sysconfig/network
  16. # Check that networking is up.
  17. [ "$NETWORKING" = "no" ] && exit 0
  18. # 特别注意,这里要调整你存放Nginx的目录
  19. nginx="/usr/local/nginx/sbin/nginx"
  20. prog=$(basename $nginx)
  21. # 特别注意,这里要调整你存放Nginx的目录
  22. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  23. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  24. lockfile=/var/lock/subsys/nginx
  25. make_dirs() {
  26. # make required directories
  27. user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  28. if [ -n "$user" ]; then
  29. if [ -z "`grep $user /etc/passwd`" ]; then
  30. useradd -M -s /bin/nologin $user
  31. fi
  32. options=`$nginx -V 2>&1 | grep 'configure arguments:'`
  33. for opt in $options; do
  34. if [ `echo $opt | grep '.*-temp-path'` ]; then
  35. value=`echo $opt | cut -d "=" -f 2`
  36. if [ ! -d "$value" ]; then
  37. # echo "creating" $value
  38. mkdir -p $value && chown -R $user $value
  39. fi
  40. fi
  41. done
  42. fi
  43. }
  44. start() {
  45. [ -x $nginx ] || exit 5
  46. [ -f $NGINX_CONF_FILE ] || exit 6
  47. make_dirs
  48. echo -n $"Starting $prog: "
  49. daemon $nginx -c $NGINX_CONF_FILE
  50. retval=$?
  51. echo
  52. [ $retval -eq 0 ] && touch $lockfile
  53. return $retval
  54. }
  55. stop() {
  56. echo -n $"Stopping $prog: "
  57. killproc $prog -QUIT
  58. retval=$?
  59. echo
  60. [ $retval -eq 0 ] && rm -f $lockfile
  61. return $retval
  62. }
  63. restart() {
  64. configtest || return $?
  65. stop
  66. sleep 1
  67. start
  68. }
  69. reload() {
  70. configtest || return $?
  71. echo -n $"Reloading $prog: "
  72. killproc $prog -HUP
  73. retval=$?
  74. echo
  75. }
  76. force_reload() {
  77. restart
  78. }
  79. configtest() {
  80. $nginx -t -c $NGINX_CONF_FILE
  81. }
  82. rh_status() {
  83. status $prog
  84. }
  85. rh_status_q() {
  86. rh_status >/dev/null 2>&1
  87. }
  88. case "$1" in
  89. start)
  90. rh_status_q && exit 0
  91. $1
  92. ;;
  93. stop)
  94. rh_status_q || exit 0
  95. $1
  96. ;;
  97. restart|configtest)
  98. $1
  99. ;;
  100. reload)
  101. rh_status_q || exit 7
  102. $1
  103. ;;
  104. force-reload)
  105. force_reload
  106. ;;
  107. status)
  108. rh_status
  109. ;;
  110. condrestart|try-restart)
  111. rh_status_q || exit 0
  112. ;;
  113. *)
  114. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  115. exit 2
  116. esac

2、赋值文件执行权限
chmod a+x /etc/init.d/nginx

3、将nginx服务加入chkconfig管理列表
chkconfig —add /etc/init.d/nginx

4、设置开机自启
chkconfig nginx on

5、其他操作命令

  1. # 启动nginx
  2. service nginx start
  3. # 停止nginx
  4. service nginx stop
  5. # 重启nginx
  6. service nginx restart
  7. #查看nginx服务是否启动成功
  8. ps -ef | grep nginx

nginx常见配置

静态网站

  1. server {
  2. listen 80;
  3. server_name www.rocky.com;
  4. return 301 https://$server_name$request_uri;
  5. location / {
  6. alias /web/rocky/;
  7. }
  8. error_page 500 502 503 504 /50x.html;
  9. location = /50x.html {
  10. root html;
  11. }
  12. }

SSL配置

  1. server {
  2. listen 443 ssl;
  3. server_name www.rocky.com;
  4. ssl_certificate /web/cert/1_www.rocky.com_bundle.crt;
  5. ssl_certificate_key /web/cert/2_www.rocky.com.cn.key;
  6. ssl_session_cache shared:SSL:1m;
  7. ssl_session_timeout 5m;
  8. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  9. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  10. ssl_prefer_server_ciphers on;
  11. location / {
  12. alias /web/rocky/;
  13. }
  14. }

代理转发

  1. server {
  2. listen 443 ssl;
  3. server_name api.rocky.com;
  4. ssl_certificate 1_api.rocky.com_bundle.crt;
  5. ssl_certificate_key 2_api.rocky.com.key;
  6. ssl_session_cache shared:SSL:1m;
  7. ssl_session_timeout 5m;
  8. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  9. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  10. ssl_prefer_server_ciphers on;
  11. location / {
  12. proxy_pass http://127.0.0.1:8080/shop/;
  13. # 转发cookie
  14. proxy_cookie_path /shop /;
  15. # 域名转发
  16. proxy_set_header Host $host;
  17. proxy_redirect off;
  18. # IP转发
  19. proxy_set_header X-Real-IP $remote_addr;
  20. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  21. proxy_connect_timeout 60;
  22. proxy_read_timeout 600;
  23. proxy_send_timeout 600;
  24. }
  25. }

映射静态资源

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location / {
  5. proxy_pass http://127.0.0.1:8080/rocky/;
  6. proxy_cookie_path /crazyandrew /;
  7. client_max_body_size 1000m;
  8. }
  9. # http://locahost/image/demo1.jpg映射到/upload/image/demo1.jpg
  10. location /image/ {
  11. root /upload/image/;
  12. rewrite ^/image/(.*)$ \$1 break;
  13. }
  14. error_page 500 502 503 504 /50x.html;
  15. location = /50x.html {
  16. root html;
  17. }
  18. }

http和https共存

  1. server {
  2. listen 80 default backlog=2048;
  3. listen 443 ssl;
  4. server_name www.rocky.com;
  5. root /web/rocky;
  6. ssl_certificate 1_api.rocky.com_bundle.crt;
  7. ssl_certificate_key 2_api.rocky.com.key;
  8. }

游览器不能访问IP

执行ps aux | grep nginx,查看Nginx服务是否启动
执行netstat -ntlp,查看80端口是否分配给你Nginx
对80端口防火墙配置:firewall-cmd —zone=public —add-port=80/tcp —permanent
重启防火墙服务:systemctl restart firewalld.service

在服务器上执行 curl localhost 看是不是正常的,是正常的,就检查安全组(云服务器)、防火墙,selinux