Jacky001
V2EX  ›  问与答

请教 nginx 的配置

  •  
  •   Jacky001 · Jun 10, 2017 · 1764 views
    This topic created in 3288 days ago, the information mentioned may be changed or developed.

    我想设置一个网站,域名 abc.com 访问是指向 /var/www/html/abc.com/ 这里面的程序,访问 abc.com/other 访问的是 /var/www/html/other/ 这里的网页内容,

    server {
            listen 80;
            server_name abc.com;
            location / {
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header HOST $http_host;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_pass http://127.0.0.1:1234;
                    proxy_redirect off;
            }
            location /other/ {
                    root /var/www/html/other/;
                    index index.htm index.html;
            }
    }
    

    我用上面的配置,abc.com 能正常访问,但 abc.com/other 的访问显示 404,root 换成 alias 也不行,请问正确的配置是?

    7 replies    2017-06-11 23:18:13 +08:00
    sundong
        1
    sundong  
       Jun 10, 2017
    /var/www/html/other/ 下面放 html 没?
    若没有 随意放一个 放一个 然后 curl -I 看下
    Jacky001
        2
    Jacky001  
    OP
       Jun 10, 2017
    @sundong 放了 index.htm
    boro
        3
    boro  
       Jun 10, 2017 via iPhone
    访问的是二级目录,配置文件 Server Name 确指向是 abc.com,不行的。除非你把 other 放到 /abc 里面,但这样就不需另外配置一个站点文件。
    Jacky001
        4
    Jacky001  
    OP
       Jun 10, 2017
    @sundong
    @boro 我写错了,正确写法应该是

    ```
    location /other/ {
    root /var/www/html;
    index index.htm index.html;
    }
    ```
    sundong
        5
    sundong  
       Jun 10, 2017
    @Jacky001 我们不够细心哈 =。=
    just1
        6
    just1  
       Jun 11, 2017 via Android
    不应该用 root 要用 alias
    julyclyde
        7
    julyclyde  
       Jun 11, 2017
    说白了,nginx 没限定“ root 只能在 server 里”就是个很大的问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3085 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 00:23 · PVG 08:23 · LAX 17:23 · JFK 20:23
    ♥ Do have faith in what you're doing.