About nginx configuration to obtain static resources
習慣沉默
習慣沉默 2017-05-16 17:16:17
0
3
543

For example, for a local project, the port number should be 127.0.0.1 after starting the service. It can be accessed in this way. Then I created a new file 'a.xxxx.com.conf' under the vhosts folder of nginx. , after configuring here, my local project can be accessed through a.xxxx.com.

The problem now is that there is a directory for storing static files in the project, so what should I do to access the static files? I'm a novice, I hope someone can tell me how to solve it, or post some code for reference.

For example, I want to get the js file in the javascript directory under the web. Currently, I can get it by configuring a.xxxx.com.conf under vhosts. As follows:
location ~*/javascript/{

root /Users/xxx/WebstormProjects/myprojects/web/javascript

}

But the way I want to achieve it is actually like this. For example, I want to obtain resources under the web through img.xxx.com/javascript/a.js. How should I do this?

thanks, thanks. Take a look at the picture below based on what I described.

習慣沉默
習慣沉默

reply all(3)
左手右手慢动作

Why no one answered?
I don’t understand it very well, please tell me a little bit about what I understand

location /img {
    root /www/abc 
}

It means that when visiting a.xxxx.com/img, searching in /www/abc is equivalent to the picture being taken under /www/abc/img
Sorry for the lack of details, it is for reference only

伊谢尔伦

Your posture itself is not quite right. Do you feel that when you visit a URL, you not only want him to access your service, but also want him to access your static files. Maybe you will say that the URL of my service is different from the URL of the static file. The problem is here, for the WEB server, how does he know the difference? How is your service URL different from the URL of static resources? If it says something different, then it's the rules of your configuration file.

Usually, in dynamic programs, people set the rules like this:

Option 1

  • Please try first to see if this URL is a file or a folder. If so, please return the static resource

  • If not, please leave it to the dynamic program (service) to handle it

The corresponding configuration file may be like this:

server {
    listen 80;
    server_name a.xxx.com;
    index index.shtml index.html index.php;
    root /Users/xxx/WebstormProjects/myprojects/web/;
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        root /Users/xxx/WebstromProjects/myprojects/server/;
        fastcgi_pass  127.0.0.1:9001;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

At this time, the main directory is generally set to: /Users/xxx/WebstormProjects/myprojects/web/ And place an index.php in the web directory as the entrance to the service. Index.php can set the app's program directory to the server. (It took so long to delete the root of the php configuration section)

Option 2

As mentioned above, the main directory is the service directory, but we have rules that directories such as javascript, css, images, uploads, etc. are stored in the web directory. Therefore, we set the access at the beginning of these paths to point the directory to the web directory. .

server {
    listen 80;
    server_name a.xxx.com;
    index index.html index.shtml index.php;
    root /Users/xxx/WebstromProjects/myproject/server;
    location / {
       // server
    }
    location ~ /(javascript|css|images) {
        root /Users/xxx/WebstromProjects/myproject/web;
    }
}

Provide some ideas, but it boils down to one thing: first think about your access rules, what URLs are, and configure them accordingly.

巴扎黑

In order to answer your question, I specially registered an account

location /c/ {

  alias /a/

}

If you access the site http://location/c, you will access the site information under the /a/ directory.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template