Linux安装Nginx及配置nginx.conf方式
作者:思祺班
本文提供了在Linux系统上安装Nginx和配置nginx.conf的详细步骤,为初学者提供了便捷的操作指导和个人经验分享,适合需要搭建服务器的用户参考
Linux安装Nginx及配置nginx.conf
操作如下
**一键安装上面四个依赖** yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel **创建一个文件夹** cd /usr/local mkdir nginx cd nginx **下载tar包** wget http://nginx.org/download/nginx-1.13.7.tar.gz **解压tar** tar -xvf nginx-1.13.7.tar.gz **进入nginx目录** cd /usr/local/nginx **进入目录** cd nginx-1.13.7 **执行命令 考虑到后续安装ssl证书 添加两个模块** ./configure --with-http_stub_status_module --with-http_ssl_module **执行make命令** make **执行make install命令** make install **启动nginx服务** /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf **打开配置文件** vi /usr/local/nginx/conf/nginx.conf * *配置nginx.conf在下面 * **配置完后 重启nginx** /usr/local/nginx/sbin/nginx -s reload **可以看一下Nginx是否启动** ps -ef | grep nginx ###相关命令 安装完成一般常用命令 进入安装目录中, 命令: cd /usr/local/nginx/sbin 启动,关闭,重启,命令: ./nginx 启动 ./nginx -s stop 关闭 ./nginx -s reload 重启
注意下面的文字说明
#第一步改这个 改为root user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #vue项目的打包后的dist自己放的前端项目位置 root /www/jinghui/dist/; location /{ try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404 index index.html index.htm; } #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件 #因此需要rewrite到index.html中,然后交给路由在处理请求资源 location @router { rewrite ^.*$ /index.html last; } # /api :是代理到后端的前缀 location /api { #填写直接的内网IP地址 proxy_pass http://*.*.*.*:9001/; #proxy_pass http://&env; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header REMOTE-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; rewrite ^/api/(.*) /$1 break; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } #nacos配置 location /nacos/ { rewrite ^//(.*)$ /$1 break; #填写自己的内网IP地址 proxy_pass http://*.*.*.*:8080/nacos; proxy_set_header Host $host; proxy_set_header User-Agent $http_user_agent; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header authorization $http_authorization; } #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; } } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。