Nginx+Window搭建域名访问

193 阅读1分钟

Nginx+Window搭建域名访问的目的时,我浏览器输入一个域名网址,例如 www.gulimall.com 能够访问到我们的商城项目。首先第一点是我们没有买域名,只有一台window电脑和虚拟机

本机Ip地址: 192.168.56.1 虚拟机IP地址:192.168.56.10

一、先修改本地的host文件

修改本地的host文件,将域名和ip地址绑定在一起。因为浏览器在进行域名解析的时候,会先读取本地的host文件,如果有对应的域名和ip地址,那么直接会从host文件里面的配置来解析

image.png

这个修改hosts文件需要授权,授权之后

二、Nginx目录结构介绍

#如果没有安装tree先安装
sudo yum install tree

先得说一下Nginx的目录结构

所有的配置都会在 nginx.conf 里面,这是总配置文件

另外一个是配置server配置文件

conf.d文件夹下

Nginx目录结构

image.png

image.png

三、创建gulimall.conf

nginx.conf 这是全局nginx的配置

worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    # 请求转发到网关,88端口对应的我们网关的微服务
    upstream gulimall{
       server 192.168.56.1:88;   
    }
    
    # 整合conf.d下的后缀 为conf的所有配置文件           
    include /etc/nginx/conf.d/*.conf;
}

gulimall.conf

server {
   # 监听的端口和IP地址或者域名
   listen       80;    
   server_name  gulimall.com;   ##这里写域名最终会解析成IP地址的,一般就是当前Nginx部署的机器的ip地址

   #charset koi8-r;
   #access_log  /var/log/nginx/log/host.access.log  main;
   
   # 静态资源请求地址 
   location /static/ {
      root  /usr/share/nginx/html;
   }

   location / {
       proxy_set_header Host $host;
       proxy_pass http://gulimall;
   }

   #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   /usr/share/nginx/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;
   #}
}