nginx 安装配置

本文介绍了如何在Mac系统中通过Homebrew安装Nginx,包括解决hosts连接问题、安装流程、配置文件修改以及Nginx的常用命令。同时,文章提供了检查端口占用、启动和管理Nginx服务的方法,并给出了部分基础的Nginx配置示例。
摘要由CSDN通过智能技术生成
一、安装brew
// 如遇到raw.githubusercontent.com连接失败问题
// 找到对应的hosts文件
// 1. sudo vi /etc/hosts
// 2. i 回车编辑
// 3. 199.232.28.133 raw.githubusercontent.com
// 4. :wq! 强制保存
// 输入以下命令,下载
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

注意:如遇到,下载完成后,提示zsh: command not found: brew 运行以下操作:(路径可替换,自定义)

  1. 执行以下命令: echo “export PATH=$PATH:/opt/homebrew/bin” >> ~/.zprofile
  2. 执行命令:export PATH=$PATH:/opt/homebrew/bin
  3. 最后执行:source ~/.zprofile
二、安装nginx
  1. 安装nginx
// 安装
brew install nginx
// 默认安装位置
/opt/homebrew/etc/nginx
// 查看nginx的配置信息
brew info nginx
// 打开相关目录
open /opt/homebrew/etc/nginx
// host文件
/private/etc/hosts

在这里插入图片描述

  1. 打开访达,command+shift+g
    在这里插入图片描述

  2. 输入终端相关nginx路径 /opt/homebrew/etc/nginx
    在这里插入图片描述

  3. nginx常用命令

sudo nginx -s stop

nginx -s reload 重新加载配置
nginx -s reopen 重启
nginx -s stop 停止
nginx -s quit 退出
nginx -V 查看版本,以及配置文件地址
nginx -v 查看版本
nginx -c filename 指定配置文件
nginx -h 帮助
nginx -t 查看配置项有没有配置错误
三、nginx配置
  1. 如上找到相关nginx配置文件

  2. 将前端vue/react打包后文件夹dist/build,放入nginx文件夹内
    在这里插入图片描述

  3. 修改nginx.conf 配置文件
    在这里插入图片描述

  4. 看端口是否被占用

// 查看端口是否被占用
netstat -anvp tcp |grep 10001
// 杀掉进程
killall -9 nginx
  1. 启动nginx
sudo nginx
四、配置项参考(持续补充中)
user  admin;
worker_processes  1;

#定位全局错误日志文件
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定进程id的存储文件位置
#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;
	# 监听地址,可以是ip,最好是域名
        server_name  localhost;
	allow all;
    	deny 10.0.1.81;
	#index index.html index.htm index.php;
	#站点目录
    	#root /opt/homebrew/etc/nginx/dist/html;
	# 设置访问的语言编码
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	#add_header Content-Security-Policy "upgrade-insecure-requests;connect-src *";
	
	# 前端访问地址
        location / {
	    # FTP 目录的绝对路径, 如若采用 alias 必须以`/` 结尾
	    alias /opt/homebrew/etc/nginx/dist/;
            index  index.html index.htm;
	    try_files $uri $uri/ /manager/index.htm;
        }

	location /admin {
	     #add_header Access-Control-Allow-Origin 'https://managerstag.matangame.com' always;
	     if ($request_method = 'OPTIONS') {
		add_header Access-Control-Allow-Credentials 'true';
                add_header Access-Control-Allow-Headers 'Authorization';
           	return 204;
      	      }
	      #Access-Control-Allow-Methods 'POST, GET, OPTIONS';
	      proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              #add_header Content-Security-Policy upgrade-insecure-requests;
	      proxy_pass http://managerstag.matangame.com/;
	}


        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #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
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    
    server {
        listen       8787;
        server_name  localhost;

        location / {
	     add_header Content-Security-Policy upgrade-insecure-requests;
             proxy_pass http://managerstag.matangame.com/;
        }
    }


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

tips:如果遇到http与https混用情况,需在nginx上server层,增加配置

add_header Content-Security-Policy "upgrade-insecure-requests;connect-src *";
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值