windows:

打开配置文件:nginx文件目录/conf/nginx.conf

启动nginx服务,服务端口为8080.  将所有请求都转发给http://127.0.0.1:8082

server {
        listen       8080;
        server_name  localhost;
        #server_name _;  # 匹配所有,不管域名还是IP

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
			proxy_pass http://127.0.0.1:8082;		   # 代理地址(主要)
			proxy_set_header Host $host;               # 传真实域名
			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;# 传HTTP/HTTPS协议
        }

...

配置多个server监听不同端口

server {
        listen       8081;
        server_name  localhost;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
			proxy_pass http://127.0.0.1:8083;		   # 代理地址
			proxy_set_header Host $host;               # 传真实域名
			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;# 传HTTP/HTTPS协议
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

nginx下载地址:https://nginx.org/en/download.html

Linux:

#Linux下配置nginx的命令


#关于修改配置文件后
nginx -t    #测试配置文件语法是否正确 
nginx -s reload    #测试通过后重载配置

#关于退出(windows也适用)
nginx -s quit    #优雅停止(等请求处理完再退出)
nginx -s stop    #快速停止(强制关闭)    



#启动nginx
# 方式1:用绝对路径启动
/usr/local/nginx/sbin/nginx

# 方式2:配置环境变量后(推荐)
echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> ~/.bashrc
source ~/.bashrc
nginx  # 直接输入 nginx 启动


#检查是否启动成功
ps aux | grep nginx    # 查看进程

netstat -tulpn | grep 80    # 查看端口(默认80)

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐