前端 Nginx 入门配置使用

535 阅读2分钟

1.下载Nginx

再下载一个Notepad++ 下面的配置文件用这个软件打开

2.找到下载的目录打开nginx.conf文件

例如我的目录地址是 D:\nginx-1.16.1\nginx-1.16.1\conf\nginx.conf

配置如下

#代表被注释了的

#user  nobody;
# nginx 进程数,建议设置为等于 CPU 总核心数
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 
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
    # 虚拟主机的配置
    # 如果配置两个就写两个server以下是两个项目的例子
    server {
        # 监听端口号
        listen       80;
        # 域名可以有多个,用逗号隔开
        server_name  risk-web.xyz;
        
        #对 "/" 启用反向代理
        location / {
			proxy_pass http://localhost:8081;
			proxy_set_header Host $host;
		}
    
		location /api {
    		# 请求的后台的ip
			proxy_pass   http://10.101.104.221:8080;
			proxy_redirect off; 
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr; 
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		}
        #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;
        }
    }
    # 同上,第二个项目的虚拟机配置
    server {
        listen       8814;
        server_name  portal.xyz;
        location / {
			proxy_pass http://localhost:8814;
			proxy_set_header Host $host;
		}
		location /api {
			#  rewrite  ^.+api/?(.*)$ /$1 break;
			#   include  uwsgi_params;
			proxy_pass   http://10.101.29.117:8080;
			proxy_redirect off; 
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr; 
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          
		}
       location ~/httpEncryptTestWithEncyptedResponse {
          #rewrite  /api/prd/(.*) /api/$1 break;
          proxy_pass   http://10.101.34.153:8080;

      }
    }
}

3. 打开如下地址进行域名配置

C:\Windows\System32\drivers\etc 打开hosts文件

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost

172.25.209.106 risk-web.xyz
172.25.209.106 portal.xyz
  • 172.25.209.106是本地IP
  • 后面这段域名要和上面的配置中的server_name保持一致

获取本地IP可以通过cmd 输入命令“ipconfig”,再按“enter”键,就可以查看到IP地址。

4. 启动Nginx服务

打开nginx的所在目录cmd 输入命令 start nginx.exe 启动服务 然后就可以通过域名访问项目了

如果打开失败原因可能如下

  1. nginx服务没有开启,打开任务管理器->详细信息查看有没有nginx进程,如果有请往下看。如果没有,看下配置中的listen端口号是否被占用
  2. 本地的项目是否打开 npm run dev,本地服务也是要开启的
  3. 本地项目webpack中配置的config文件中的proxyTable要注释掉,开启了Nginx服务就可以不用webpack中的这个代理

5. 一些问题

nginx在每次修改conf后都需要重启,命令是 nginx -s reload

如果关闭了想要重新开启要去任务管理器杀掉进程

nginx -s reload 重新加载配置

nginx -s reopen 重启

nginx -s stop 停止

nginx -s quit 退出 nginx

更改线上nginx配置文件

打开SSH Secure Shell 并登陆

cd 到服务器上的nginx文件路径,如果找不到可以find nginx.conf 找到路径

vi nginx.conf //修改文件

按a开始修改,esc修改完成

cd nginx根目录

cd sbin

nginx -s reload