Nginx 从安装到实战 您所在的位置:网站首页 centos代理上网配置 Nginx 从安装到实战

Nginx 从安装到实战

2023-04-07 03:00| 来源: 网络整理| 查看: 265

Nginx 是什么 轻量级Http服务器 事件驱动的异步非阻塞处理方式框架 更好的IO性能 一般用用服务器反向代理和负载均衡 Nginx 优势 IO多路复用 : 多个描述符的IO操作都能在一个线程里并发交替顺序完成.复用线程 CPU亲和 : 把CPU核心和Nginx工作进程绑定,把每个worker进程固定在一个cpu核上执行 sendfile: 零拷贝传输模式,不需要经过用户空间 准备 购买服务器 付费模式 : 按量付费 架构 : x86计算 分类 : 共享型 镜像 : CentOS 7.6(64位) 带宽计费模式: 固定带宽 登录凭证 : 自定义密码 通过公网IP连接到服务器

操作 安装依赖模块 yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake yum -y install wget httpd-tools vim 安装Nginx方式一 (自动安装, 但安装的版本无法自定义 ,可以先配置nginx安装源地址) 安装源 #创建安装源配置 vim /etc/yum.repos.d/nginx.repo #填写内容 [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 #保存退出 wq! #安装nginx yum install nginx -y #查询nginx的安装路径 rpm -ql nginx nginx目录介绍 nginx操作 #启动nginx systemctl start nginx #停止nginx systemctl stop nginx #nginx 从新加载配置文件 nginx -s reload #nginx 检查语法是否正确 nginx -t #查看nginx 进程 ps -ef | grep nginx 阿里云服务启动nginx后,访问对应服务器公网ip 仍是502解决方法 - 原因是 刚买的阿里云服务器 安全组中 没有配置80端口

- nginx配置目录

/etc/nginx/conf.d/ #创建的文件以 ".conf"结尾

* 特别注意 : 每个".conf"文件中的 listen (监听的端口号) 必须唯一 . 否则会报 nginx错误为 : server_name 重复

nginx 配置文件内容解析 server { listen 80; //监听的端口号 server_name localhost; //用域名方式访问的地址 #charset koi8-r; //编码 #access_log /var/log/nginx/host.access.log main; //访问日志文件和名称 location / { root /usr/share/nginx/html; //静态文件根目录 index index.html index.htm; //首页的索引文件 } #error_page 404 /404.html; //指定错误页面 # redirect server error pages to the static page /50x.html # 把后台错误重定向到静态的50x.html页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # 代理PHP脚本到80端口上的apache服务器 #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # 把PHP脚本9000端口上监听的FastCGI服务 #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # 不允许访问.htaccess文件 #location ~ /\.ht { # deny all; #} }\ } nginx配置访问html,css,js文件访问 location ~ .*\.(html|js|css)$ { root /usr/project/static; # 缓存时间 expires 1h; # 启用文件压缩 gzip on; # 压缩版本 gzip_http_version 1.1; # 压缩等级 gzip_comp_level 2; # 采用什么类型的压缩 gzip_types application/javascript text/css; } nginx配置图片文件访问 location ~ .\.(jpg|png|jpeg|svg)$ { #设置防盗链 防止别的网址引用图片 #参数一:允许默认打开 #参数二:是否协议访问 #参数三: 自己的服务器公网ip valid_referers none blocked http://10.156.15.19; i($valid_referers){ return 403; } #设置图片路径 root /usr/project/static/img; } nginx配置json文件访问 location ~ .*\.json$ { #允许跨域 add_header Access-Control-Allow-Origin *; #允许访问方法 add_header Access-Control-Allow-Methods GET,POST; #允许访问携带的头属性 add_header Access-Control-Allow-headers Contet-Type; #设置json路径 root /usr/project/static/json; } nginx配置Vue #/etc/nginx/conf.d/ 目录下创建一个 vuepage.conf #vue代码上传至/user/local/www目录 #编辑以下内容 server { charset utf-8; listen 8900; server_name 127.0.0.1; autoindex off; add_header Cache-Control "no-cache, must-revalidate"; location / { root /usr/local/www/dist; add_header Access-Control-Allow-Origin *; try_files $uri $uri/ @router; index index.html index.htm; } location @router { rewrite ^.*$ /index.html last; } } nginx配置代理 正向代理 : 用户与代理在一起(用户知道自己开启了代理) 进行的访问 ,例如科学上网 反向代理 : 代理与服务器在一起(用户无感知), 例如:用户通过域名访问服务器,代理根据不同域名访问服务器上的不同服务.然后呈现给用户 #配置反向代理 /etc/nginx/conf.d server { listen 80; #已备案的域名 server_name www.a.com; location / { #本地服务 proxy_pass http://localhost:3000; } } server { listen 80; #已备案的域名 server_name www.b.com; location / { #本地服务 proxy_pass http://localhost:4000; } } nginx负载均衡 #/etc/nginx/conf.d #声明 apiserver upstream apiserver { #weight权重 . 限制访问的数量 #ip_hash 让用户固定在某一个服务上; ip_hash; server localhost:3000 weight=2; server localhost:4000 weight=1; server localhost:5000 weight=3; } server { listen 80; #已备案的域名 server_name www.c.com; location / { #使用自定义的变量名称(apiserver) 作为代理访问 proxy_pass http://apiserver; } } nginx移动端适配 location / { if($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') { set $mobile_req '1'; } #如果是移动端访问 将网址重定向到 'http://h5.com' if($mobile_req = '1'){ rewrite ^.+ http://h5.com; } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有