nginx环境配置Windows本地测试,测试打包后的代码_nuxt2 如何配置nginx

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!


#user  nobody;
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 {
    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  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        #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       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


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

}

我这配置是本地访问localhost,在浏览器直接输入localhost 就可以访问了,

但是,只要你刷新浏览器就会报404;

原因在于少一句代码:

location / {

try_files $uri $uri/ /index.html;

}

方法②:启动:在主目录下打开DOS命令行窗口,输入: start nginx

关闭:输入:nginx -s stop

推荐使用第二种方法,第一种方法关闭的时候太麻烦

常用到的命令如下:

nginx -s stop       快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
    nginx -s quit       平稳关闭Nginx,保存相关信息,有安排的结束web服务。
    nginx -s reload     因改变了Nginx相关配置,需要重新加载配置而重载。
    nginx -s reopen     重新打开日志文件。
    nginx -c filename   为 Nginx 指定一个配置文件,来代替缺省的。
    nginx -t            不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
    nginx -v            显示 nginx 的版本。

nginx -V            显示 nginx 的版本,编译器版本和配置参数。

另外,如果想每次都手动敲命令开启和关闭nginx,可以在nginx安装目录下新添一个启动批处理文件startup.bat,双击即可运行。内容如下:

@echo off
rem 如果启动前已经启动nginx并记录下pid文件,会kill指定进程
nginx.exe -s stop
 
rem 测试配置文件语法正确性
nginx.exe -t -c conf/nginx.conf
 
rem 显示版本信息
nginx.exe -v
 
rem 按照指定配置去启动nginx
nginx.exe -c conf/nginx.conf

接下来就是实践了,仅仅为完成一个反向代理

①先修改nginx.conf配置文件,如下:

#user  nobody;
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 {
    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)
	  client_max_body_size 10m;  #允许客户端请求的最大单文件字节数 
	  client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
      proxy_redirect off;
      proxy_set_header Host $host;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
      proxy_set_header X-Real-IP $remote_addr; # 真实的客户端IP
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 代理路由信息,此处取IP有安全隐患
	  proxy_set_header   X-Forwarded-Proto $scheme;# 真实的用户访问协议
	  proxy_connect_timeout 65;#nginx跟后端服务器连接超时时间(代理连接超时)
      proxy_send_timeout 65; 
      proxy_read_timeout 65;#连接成功后,后端服务器响应时间(代理接收超时)
      proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
      proxy_buffers 4 32k;#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
      proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
	  proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
	  
	#负载均衡组
	#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
	#静态服务器组 
	upstream staticCluster{
            server 192.168.0.144:8088 weight=1;
    }
	#动态服务器组
	upstream dynamicCluster{
			#ip_hash;
			server  192.168.0.144:8088 weight=1;
			#server  192.168.0.144:8089 weight=1;
    }
 
     server {
		#监听端口
        listen       80;
		#域名可以有多个,用空格隔开
        server_name  localhost;
		
        #charset koi8-r;
		
		#设置编码为utf-8;
		charset    utf-8;
		
		#nginx访问日志
        #access_log  logs/host.access.log  main;
 
        #location / {            
        #    index  login.html;
        #}
		#页面地址
		set $dz /dev/opt/web/demo;
		
		location = / {
				root $dz;
				index  login.html;
		}	
		
		#静态文件交给nginx处理
		location ~* \.(htm|html|json|gif|jpg|jpeg|png|bmp|swf|ico|rar|zip|txt|flv|mid|doc|docx|ppt|pdf|xls|xlsx|mp3|wma|eot|svg|ttf|woff)$ {
				#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移
				#proxy_next_upstream http_502 http_504 error timeout invalid_header;
				#proxy_cache cache_one;
				
				#对不同的HTTP状态码设置不同的缓存时间
				proxy_cache_valid 200 304 302 5d;
				#proxy_cache_valid any 5d;
				
				#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
				proxy_cache_key '$host:$server_port$request_uri';
				
				add_header X-Cache '$upstream_cache_status from $host';
				#静态服务器组
				#proxy_pass http://staticCluster;
					
				#所有静态文件直接读取硬盘
                root $dz;
                expires 30d; #使用expires缓存模块,缓存到客户端30天
            }
		
		#静态文件交给nginx处理
		location ~* \.(js|css)$ {
					root $dz;
					expires 1h;
				}
 
		#动态文件交给tomcat/jetty处理
		 location ~*$ {
		 
					 proxy_intercept_errors on; # 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面
                     #proxy_pass http://127.0.0.1:8088;
					 proxy_pass http://dynamicCluster;
           }
		#fastcgi_intercept_errors on; 
		# 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面 
        error_page  404   /login.html;
		location = /login.html {
				root $dz;
				index  login.html;
        }
		
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


![img](https://img-blog.csdnimg.cn/img_convert/90e97c384885ca157310e4e5598bff00.png)
![img](https://img-blog.csdnimg.cn/img_convert/5b8784075028342a15ad289bd34fd67b.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618631832)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**


            root   html;
        }


[外链图片转存中...(img-hDrHBwDH-1715811102282)]
[外链图片转存中...(img-ih5HX5dH-1715811102283)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618631832)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

当你使用`pm2`管理和部署了基于Nuxt.js v3构建的项目后,要在Nginx上设置域名并启用HTTPS,需要按照以下步骤进行: 1. **配置Nginx服务器**: - 首先,你需要在Nginx配置文件(通常位于 `/etc/nginx/sites-available/your-project.conf` 或 `~/nginx/conf.d/`)中创建一个新的站点配置2. **添加域名块**: ``` server { listen 80; # 网站监听80端口 server_name your-domain.com; # 替换为你想要绑定的域名 # 使用HTTP跳转到HTTPS location / { return 301 https://$host$request_uri; } # 关闭默认的网站目录访问 root /path/to/your/nuxt/dist; # 替换为你的Nuxt应用实际打包后的路径 try_files $uri $uri/ @nuxt; } ``` 3. **启用HTTPS**: - 对于HTTPS支持,你需要生成SSL证书。可以购买商业证书或通过Let's Encrypt免费获取。将证书文件(如`cert.pem`, `key.pem`)放到合适的路径下。 4. **更新Nginx配置以包含HTTPS**: ``` server { listen 443 ssl; # HTTPS监听443端口 server_name your-domain.com; ssl_certificate cert.pem; # 你的SSL证书路径 ssl_certificate_key key.pem; # 私钥路径 # 其他SSL选项(如加密套件等),这里省略 include snippets/ssl-params.conf; # Nuxt的路由配置继续在这里,保持@nuxt部分不变 location @nuxt { proxy_pass http://your-ip:your-port; # PM2运行的Nuxt应用的IP地址和端口号 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Nginx-Proxy true; } } ``` 5. **重启Nginx服务**: ``` sudo service nginx restart ``` 6. **验证配置**: - 使用`curl`或浏览器访问`https://your-domain.com`确认配置是否生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值