NGINX代理的配置(简单易用版本)

NGINX代理的配置(简单易用版本)

首先说下nginx把,这个其实也是作为代理的,之前用的是vuecli3/4进行的代理配置,之前一直没有调通(后来必须要弄明白,可以看我的另一个文章。),就临时采用了nginx的方式进行本地的代理,我这里的代理方式是针对前端的开发模式的,因为针对服务器的比较多,所以我就不一一说明了。

首先需要下载nginx的项目工具,我用的版本是nginx-1.16.1
找到在当前目录下的/config/nginx.conf文件 ,用编辑器打开。
这里要区分一下你的需要代理的是http还是Https,我这里以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 {
        	listen       80;
        	server_name  www.baidu.com;
        	#charset koi8-r;
        	#access_log  logs/host.access.log  main;
        	location / {
            	proxy_pass http://127.0.0.1:8088; #代理项目部署的地址(这里项目部署在了当前服务器tomcat上,端口8080)
				proxy_set_header   X-Forwarded-For $remote_addr;
            	proxy_set_header   Host $http_host;
        	}
        	location /api{
           		proxy_pass http://192.168.0.7; #代理项目部署的地址(这里项目部署在了当前服务器tomcat上,端口8080)
            	proxy_redirect off;
            	proxy_set_header   Host $host;
            	proxy_set_header   X-Real-IP $remote_addr;
            	proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            	proxy_set_header   X-Forwarded-Host $server_name;
        	}
        #		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;
        #}
    }

这里的核心是配置server对象,
listen : 服务器的端口号
server_name:服务器的域名或者地址(没有http://)
location : 如果location后面只有一个“/”,代表着,直接访问域名的情况下会使用的实际链接
proxy_pass : 需要访问的链接(这里需要带上http://)

其次就是配置本地的hosts:
hosts文件所在的目录:C:\Windows\System32\drivers\etc
需要增加的内容

# 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
   127.0.0.1     www.baidu.com
 host.docker.internal
 gateway.docker.internal
192.168.5.102 host.docker.internal
192.168.5.102 gateway.docker.internal
# Added by Docker Desktop
192.168.0.44 host.docker.internal
192.168.0.44 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

理论上配置号这些,就可以去启动nginx了,启动的方式有两种,
一个是cmd cd到nginx的目录下,输入start nginx
另一个就是直接双击nginx.exe
之后就可以使用了,而且有一点需要记住的是,如果是用命令行进行控制,不要使用powershell,用cmd

你可能感兴趣的:(代理,nginx,路径)