nginx接口转发/代理/单页面应用配置

README.md
复制

关于nginx的小小笔记

不常用,没过一段时间都要去搜一下,干脆自己记下来

重启nginx服务器

/etc/init.d/nginx restart
# or
service nginx restart

在新增/修改配置之后,需要重启服务器

如果遇到问题:

[....] Restarting nginx (via systemctl): nginx.serviceJob for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
 failed!

按照指示输入systemctl status nginx.service

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since 五 2017-06-09 16:02:56 CST; 44s ago
  Process: 3988 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 3570 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 4061 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
 Main PID: 3575 (code=exited, status=0/SUCCESS)

6月 09 16:02:56 axetroy-H81M-DS2 systemd[1]: Stopped A high performance web server and a reverse proxy server.
6月 09 16:02:56 axetroy-H81M-DS2 systemd[1]: Starting A high performance web server and a reverse proxy server...
6月 09 16:02:56 axetroy-H81M-DS2 nginx[4061]: nginx: [emerg] invalid parameter "on" in /etc/nginx/conf.d/kaopu-proxy.conf:13
6月 09 16:02:56 axetroy-H81M-DS2 nginx[4061]: nginx: configuration file /etc/nginx/nginx.conf test failed
6月 09 16:02:56 axetroy-H81M-DS2 systemd[1]: nginx.service: Control process exited, code=exited status=1
6月 09 16:02:56 axetroy-H81M-DS2 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
6月 09 16:02:56 axetroy-H81M-DS2 systemd[1]: nginx.service: Unit entered failed state.
6月 09 16:02:56 axetroy-H81M-DS2 systemd[1]: nginx.service: Failed with result 'exit-code'.

关键字 nginx: [emerg] invalid parameter "on" in /etc/nginx/conf.d/kaopu-proxy.conf:13

api-proxy.conf
复制


# located: /etc/nginx/conf.d/api-proxy.conf
# 代理api
# http://localhost:3000/api/v1/user > http://192.168.8.100:8080/api/v1/user
server {
    listen 3000;

    root /var/www/your_site;

    location ~ ^/(api) {
        proxy_pass http://192.168.8.100:8080;
        proxy_redirect off;
    }
}


forwarding.conf
复制


# located: /etc/nginx/conf.d/forwarding.conf
# 端口转发
# localhost:3000 > 192.168.8.100:8080
server {
    listen 3000;

    server_name http_proxy;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://192.168.8.100:8080/;
        proxy_redirect off;

        # Socket.IO Support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}


spa.conf
复制


# located: /etc/nginx/conf.d/spa.conf
# 单页面应用
server {
    listen 3000;

    root /var/www/your_site;

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


大牛们的评论:朕有话说

还没有人评论哦,赶紧抢沙发!